Dead: // Need help on programming question.

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
Originally posted by: Coldkilla
Ah, I think I sorta see how it could be done but I don't have much time now before the exam to be 100%.

Thanks all for your time.

Original Post:
Since we started learning arrays, I've had a hell of a time trying to understand them. Our instructor gave us a "study" packet to practice but this one I completely skipped over and I have no idea how to rearrange an array! The final is today!! Here's the question:

Write a the definition for a function that takes an array of doubles, the array?s size and a
location (index value) as its three parameters and returns the index of the smallest value in
array via the location parameter.

I'll be refreshing the page every few minutes to possibly strike a small conversation (if possible) before the final soon.

Any help appreciated. Thanks.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Do you have anything so far or are you expecting everyone else to do all the work?
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Originally posted by: Coldkilla
Originally posted by: nickbits
http://tinyurl.com/re49py

And how would you recommend I find how to address my question in a single search bar? I have no idea.

I'll try and get some code up, I apologize for the lack of information. I'm just a little frantic at the moment.

Read up on arrays. Then you can answer the question yourself.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Coldkilla
Originally posted by: nickbits
http://tinyurl.com/re49py

And how would you recommend I find how to address my question in a single search bar? I have no idea.

I'll try and get some code up, I apologize for the lack of information. I'm just a little frantic at the moment.

Not to speak for nick, but he would probably say that if you follow the some of those links it will quickly become apparent how to scan an array for the minimum value. Anyway, don't worry about it. We're a little sensitive to people trying to get their homework done, but you seem to be in a spot. My advice for next time is don't skip stuff. Programming, like programs, consists of big ideas constructed out of lots of smaller ones.

Anyway, sounds like what your prof wants is pretty straightforward. He wants you to write a function that will take an array, it's size, and return the index of the smallest value in the array. For this you need to know: the basics of constructing a function and returning a value; how to access a value in an array by index or pointer or both; how to loop over all the contents of an array of given size; how to use a local variable to keep track of the smallest value you find.

Oh, and given the way he worded it he may want you to return the value via a reference argument, rather than as the return value from the function.
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
Edit: Here's some code I have that:
-Declares an array of floats.
-Sets each element in the array to a random number.
-Prints the array elements to the screen.
-Sorts the array (in ascending order).
-Prints the array elements to the screen again.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int findm(const float vals[], int s, int e);
void swap(float &x, float &y);
void sort(float vals[], int eSize);
void print(float vals[], int eSize);


int main()
{
... srand(time(NULL));

... const int Size = 10;
... float scores[Size];

... for (int s = 0; s < Size; ++s)
...... scores = rand() % 101;

. cout << "Scores" << endl;
. cout << "------" << endl;
. print(scores, Size);
. cout << endl << endl;

. sort(scores, Size);

. cout << "Scores" << endl;
. cout << "------" << endl;
. print(scores, Size);
. cout << endl << endl;

. return 0;
}


int findm(const float vals[], int s, int e)
{
... int ml = s;
... for (int i = s; i <= e; ++i)
...... if (vals[ I ] < vals[ml])
......... ml = i;

. return ml;
}


void swap(float &x, float &y)
{
... float tmp = x;
... x = y;
... y = tmp;
}

void sort(float vals[], int eSize)
{
... for (int i = 0; i < eSize; ++i)
...... swap(vals[ I ], vals[findm(vals, i, eSize-1)]);
}

void print(float vals[], int eSize)
{
... for (int i = 0; i < eSize; ++i)
...... cout << vals[ I ] << endl;
}


Here's a picture with the code formatted:
http://img359.imageshack.us/img359/5728/23824129.jpg

I figure this would be the only code I have that could (probably easily) be converted into addressing the original question).
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
Ah, I think I sorta see how it could be done but I don't have much time now before the exam to be 100%.

Thanks all for your time.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,817
75
Originally posted by: presidentender
Did you do the homework for the rest of the class?
I somehow doubt it. :evil:

Did you do the homework throughout the rest of the class?
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
Originally posted by: Ken g6
Originally posted by: presidentender
Did you do the homework for the rest of the class?
I somehow doubt it. :evil:

Did you do the homework throughout the rest of the class?

See, this is why we need a formal spec for well-formed, well-defined English. Never would've been any ambiguity in a programming language.
 
Sep 29, 2004
18,656
68
91
I remember when I was in school. If I spent the semester learning the various topics in a class, I didn't even bother studying for tests. If I had to cram before a test, I knew I was already screwed. Best to learn this lesson early and fast.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Wait. Do you even understand the sort code you posted?

findm does exactly what you're asking for in the OP; in fact it's more general than what you wanted.