# Hey Guys, can anybody help?

• 01-03-2012
MstrKurt
Hey Guys, can anybody help?
Hey. First post here, was wondering whether anybody can help me.

I have coded a for loop to ask for a sequence of numbers and enter them in to an array, and then ask what the user would like to do with them: I have managed to get it to calculate the average, but i'm unable to make it find the max number entered. The only way I can think of is by using if commands to check whether each number is greater or equal to the next, but that seems like a lot of coding. Which way would you guys suggest?
Thanks :)
• 01-03-2012
anduril462
Iterate through the array (using a for loop) and check for it in the loop. You'll use just one if statement that way. Also, you could do this in the loop where you get the numbers from the user.
• 01-03-2012
MstrKurt
Code:

```if (number==3)                                                                    {     printf("Max Number:"); max=0; if (max < vc[i]) { vc[i] = max ;     printf("Your largest number was: %d", vc[i]); } }```
I come up with that but doesn't work.
• 01-03-2012
anduril462
Why is it inside an if (number==3) block? Since number is not a descriptive variable name, I have no idea what that if statement actually does. Can I see your whole code? Also, you probably want to reverse your assignment, i.e. [I]max = vc and to print max.
• 01-03-2012
MstrKurt
Code:

```#include <stdio.h> void main() { int valc[5];    //valc = Value Count (Amount of Numbers Entered). int number;        //Number for what calculation the user requests. float average;    //Average int n;            //Variable Number for For Loop int max=0;        //Max Number float sum;        //the sum of the inputs for (n=0;n<5;n++) { printf("Please enter your values\n\n"); scanf("%d",&valc[n]); printf("\n"); } printf("\nWhat would you like to calculate from these numbers?\n\n");     printf("1. Find the Average of the numbers entered\n");     printf("2. Find the Maximum number entered\n");                                //User enters what they are looking for.     printf("3. Find the Minimum number entered\n");     printf("4. Find the Standard Deviation of the numbers entered\n\n"); scanf("%d", &number);                                                            //Scans the number the user has input. if (number==1)                                                                    //If function that checks if the input was equal to 1. { printf("\nFind the Average of the numbers entered\n\n"); sum=valc[0]+valc[1]+valc[2]+valc[3]+valc[4];                                    //Sum of all 5 inputs. average=sum/5;                                                                    //Sum divided by the amount of inputs. printf("The Average of the values entered was: %.2f\n\n",average);                //Result of calculation } if (number==2)                                                                    //If function that checks if the input was equal to 2. {     printf("\nFind the Maximum number entered\n\n"); } if (number==3) {     printf("\nFind the Minimum number entered\n"); } if (number==4) {     printf("\nFind the Standard Deviation of the numbers entered\n"); } }```
• 01-03-2012
anduril462
It's int main(void) and return an int at the end, usually 0. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

Ahh, so number really represents the user's menu choice. Why not call it menu_choice then? Seems like a good, descriptive name to me.

It's clear you know how to use arrays with for loops, since that's how you get the data from the user (unless you were given that code or found it on the web). So why don't you use it when calculating the sum for the average? You should actually be using a for loop inside each of those if (menu_choice...) statements.

So I'll start you off for the max, and let you finish it. Min is similar. Note you were close in post #3:
Code:

```for (n = 0; n < 5; n++) {     if (max < valc[n]) {         // Fill in this line according to my suggestion in post #4     } }```
• 01-03-2012
MstrKurt
Thank you for your help :) I have ammended the code as follows:
Code:

```     for (n = 0; n < 5; n++)    {    if (max < valc[n])     {      max=valc[n];       printf("Your largest number was: %d", max);                                // Fill in this line according to my suggestion in post #4    }```
But it prints every value I enter
• 01-03-2012
anduril462
Okay, so think about it for a minute. Where is the printf located. Where should it be if you want it to print just once? You have to read the code and "be the computer". Run through the code in your head or on a piece of paper. Figure out why it's doing what it's doing and how to change it to do what you want.
• 01-04-2012
Salem
Next time -> bespecific
• 01-04-2012
Jaikrishna
First get all the values as you did before.
Then if user selects to display the maximum number
perform
Code:

```max=valc[0] for(n=1 ; n<5 ; n++)     if(valc[n]>max)         max=valc[n]; printf("The maximum is %d",max);```
• 01-04-2012
CommonTater
Quote:

Originally Posted by Jaikrishna
First get all the values as you did before.
Then if user selects to display the maximum number
perform
max=valc[0]
for(n=1 ; n<5 ; n++) if(valc[n]>max) max=valc[n];
printf("The maximum is %d",max);

1) Please use code tags when posting source code.

2) Learn to format code for readbility... never put multiple commands on a single line.

Like this...
Code:

```max=valc[0]; for(n=1 ; n<5 ; n++)   if(valc[n]>max)       max=valc[n]; printf("The maximum is %d",max);```
• 01-04-2012
Jaikrishna
Sorry
Sorry, That was my first post :)
Now learnt and corrected.
• 01-04-2012
CommonTater
Quote:

Originally Posted by Jaikrishna
Sorry, That was my first post :)
Now learnt and corrected.

You're still missing a semi-colon on the first line. (not that it really matters in a trivial example.)

Also, it's kind of unhelpful to go back and edit past posts around here. It destroys context, leaving people wondering what the heck is going on.

Welcome aboard, by the way...
• 01-05-2012
MstrKurt
Quote:

Originally Posted by anduril462
Okay, so think about it for a minute. Where is the printf located. Where should it be if you want it to print just once? You have to read the code and "be the computer". Run through the code in your head or on a piece of paper. Figure out why it's doing what it's doing and how to change it to do what you want.

Ok i've managed to fix it :) Thanks a lot for your help :). I've learnt a lot from just this one problem!.