Thread: Array Average Issue

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    86

    Array Average Issue

    Hello,

    I am new to C and the forum. I am taking my first class in programming and am a little stuck in the assignment.

    The guidelines are to write a program that allows a user to input numbers into an array to be averaged. I am supposed to use the argv function from main and not use scanf (this part is really throwing me off). Lastly it is supposed to remind the user if they forget to enter an interger ( I am not that far yet so that is an non-issue)

    So my code at the time being is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int *argv[]){
        
        int i;
        int sum = 0;
        float average;
        
        printf ( "Welcome to the Average Program. \n");
        
        for ( i = 0; i < 0; i++) {
            printf ( " Enter the numbers you would like to average: ");
                   scanf ("%d", &argv[i]);
                   sum += arr[i];
            }   
            
            average = (float)sum/argv[];
            printf ("\n The average is %.lf\n", average);
            
        
        getchar();
        
        return 0;
        
    }
    I have comipled the program without any errors but it does not run. So I know I have went awry somewhere.

    What I THINK the program was doing:

    1. Declaring the array.
    2. Running it through a for-loop to received the inputted numbers from the user.
    3. Averaging and printing the user-inputed numbers

    Thanks for your help, any pointers on format or anything else I am missing is appreciated. I am here to learn.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    • Welcome to the forum
    • argv is not a function.It is actually a parameter of function main.Usually main has two arguments(parameters) argv and argc or no parameters at all.Let's for now stick to only one parameter,argv.What is argv?argv is an array of characters.
    • You have written
      Code:
      int main(int *argv[])
      but you should write it like this
      Code:
      #include <stdio.h>
      
      int main (int argc, char *argv[])
      {
      
        return 0;
      }
      argc is the number of arguments your program has and argv holds these arguments. I suggest you looking here The GNU C Programming Tutorial
    • Code:
       for ( i = 0; i < 0; i++)
      First look here a bit For loop - Wikipedia, the free encyclopedia and then think that we set i to zero and what we actually say to our program is "Execute the body of this loop until i is smaller than zero".But i is equal to zero already,so the body of this loop will never be executed.
    • Code:
      scanf ("%d", &argv[i]);
      scanf is for requesting data from the user.But you have to use argv,thus command line.Did i understood correct?If so this line must be removed.
    • Code:
      sum += arr[i];
      This line does not produce any errors???????????What is arr[i]?I am looking at your code and i do not see any declaration with this name !
    • Code:
      average = (float)sum/argv[];
      What does this line do?I think it is not correct.
    • You said that you think that the program is declaring an array..I see only these declerations
      Code:
          int i;
          int sum = 0;
          float average;
      none of them is an array.Remember that argv is an argument of a function(main function)

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Thanks for the pointers and resources, but they left me with a few questions.

    Can I used argv[] to store the user input or do I have to declare an another array?
    Also, how are numbers stored using this as opposed to the scanf route?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Basically, argc is an integer that contains the number of arguments passed to the program and argv is an array of the arguments that was passed.
    argv[0] is always the program name itself.

    You write them at command line and program automatically stores the data into argv.For example,if i write
    myProgram 1 2 3
    then argc=4
    and argv[0] contains myProgram
    argv[1]=1
    argv[2]=2
    argv[3]=3

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Got it. Thanks. Believe that straightened me out, I learned quite a bit rather quickly, but I am unsure how to prompt the user to input intergers if they try to run the program without doing so.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    if (argc != EXPECTED_AMOUNT)
    {
        init_self_destruct();
        fputs("Self Destruction Initiated", stderr);
        return 1; //Exit Failure from main and run away.
    }

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    ....
    Last edited by LearnOnTheFly; 09-03-2012 at 08:36 PM.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    I guess I should of clarified it should just tell the user to enter integers if they try to run the program without entering values. We do not have to write for any unexpected inputs

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    LearnOnTheFly:

    ....
    Yes - I need to get out more.

    What I was trying to say was that you could check that the inputs are correct, and only prompt them if they get it wrong (by checking the value argc has).

    If in Windows Command Prompt you call ipconfig with incorrect inputs, it prompts you with "Error: unrecongnized or incomplete command line" and then displays help information of what it expects.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by std10093 View Post
    Code:
     for ( i = 0; i < 0; i++)
    First look here a bit For loop - Wikipedia, the free encyclopedia and then think that we set i to zero and what we actually say to our program is "Execute the body of this loop until i is smaller than zero".But i is equal to zero already,so the body of this loop will never be executed.
    Not quite, the body of the for-loop is executed while the condition is true, not until it is true.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    I believe the average part of the program works and is correct here:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]){
        
        int i;
        int sum = 0;
        float average;
        
        printf ( "Welcome to the Average Program. \n");
        
        for ( i = 0; i < argc; i++) {
            sum += i;
            
        } 
            
            average = (float)sum/argc;
            printf ("\n The average is %.lf\n", average);
            
        
        getchar();
        
        return 0;
        
    }
    To finish though, I need to alert the user if they do not enter any integers (I do not have to plan the program for incorrect input such as char, double, etc) just if they attempt to run the program without entering any numbers.

    I'm not quite sure how to go about this and any input would be appreciated.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Click_here already suggested that you check the value of argc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "I believe the average part of the program works and is correct here:"
    Code:
        for ( i = 0; i < argc; i++) {        sum += i;            }
    I don't think that this is working for you correctly - Didn't you want to take the average of what was being put in? argv[i]?...

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    You're right click, thank you. I was inputting 1 2 3 4 5 as on my command line and was receiving the right answer. But when I used random integers I obviously got the wrong number.

    How do I find the sum of the inputs in argv? I'm unsure how to pass the values to aquire the correct sum converting the char argv to int.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by carpeltunnel View Post
    How do I find the sum of the inputs in argv? I'm unsure how to pass the values to aquire the correct sum converting the char argv to int.
    First you should start your index i with 1 instead of 0 because argv[0] is the name of the program.

    For converting a string to int read the FAQ.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Average of an Array
    By Peteski in forum C Programming
    Replies: 19
    Last Post: 01-05-2012, 04:38 PM
  2. average of an array
    By mrsirpoopsalot in forum C Programming
    Replies: 20
    Last Post: 09-16-2006, 04:32 PM
  3. average of array elements?
    By swapnil_sandy in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 12:16 PM
  4. average in an array
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 01:03 PM
  5. average of total using array
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 05:27 PM