Like Tree1Likes
  • 1 Post By CommonTater

Hey Guys, can anybody help?

This is a discussion on Hey Guys, can anybody help? within the C Programming forums, part of the General Programming Boards category; Hey. First post here, was wondering whether anybody can help me. I have coded a for loop to ask for ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    27

    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

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    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.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    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.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    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");
    }
    
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    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
        }
    }

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    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

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    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.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Next time -> bespecific
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    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);
    Last edited by Jaikrishna; 01-04-2012 at 07:01 AM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jaikrishna View Post
    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);
    Jaikrishna likes this.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Sorry

    Sorry, That was my first post
    Now learnt and corrected.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jaikrishna View Post
    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...

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    27
    Quote Originally Posted by anduril462 View Post
    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!.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-05-2008, 09:04 PM
  2. Thanks Guys...
    By frodonet in forum C Programming
    Replies: 0
    Last Post: 11-03-2007, 08:16 PM
  3. do you guys have...
    By enjoyincubus in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 09:14 PM
  4. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 04:47 PM
  5. Hey Guys! I Need Some Help Here!
    By Ruski in forum C++ Programming
    Replies: 6
    Last Post: 06-27-2002, 02:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21