Thread: Array unfamiliarities

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    98

    Array unfamiliarities

    Alright I am supposed to write code that allows the user to enter inputs for the array grade that are counted as they are read. The program is to display the average and sum of the array. Also, the program is to display the numbers in the array with an * if they are below the average along with the letter grade (i.e. 92 is A).

    I have wrote the parts of the code that I am familiar with. The two things that my text does not cover, is how to establish an array and its size through user input. And how to add the * and letter grade to the displayed list after the computations are done.

    Here is what code I do have:

    Code:
    #include <stdio.h>
    #pragma warning (disable :4996)
    double findSum(double[], int);
    double findAvg(double [], int);
    int main()
    {
    #define NUMELS
      
      double grades;
      double average, sum;
      printf("Enter the grades, when finished enter a negative number:  ");
      scanf("%f", &grades[]);
      
      average = findAvg(grades[], int);
      sum = findSum(grades[], int);
      printf("The sum of the grades is %f", sum);
      printf("The average of the grades is %f", average);
      return 0;
    }
    double findAvg(double grades[], int)
    {
      int i;
      double sumnums = 0.0;
      for (i = 0; i < NUMELS; i++)
       sumnums = sumnums + grades[i];
      return (sumnums/NUMELS); /*calculate and return average*/
    }
    double findSum(double grades[], int)
    {
      int i;
      double sumnums = 0.0;
      for (i = 0; i < NUMELS; i++)
       sumnums = sumnums + grades[i];
      return (sumnums); /*reutrn the sum*/
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    The only way to determine array size based off user input is through malloc(dynamic memory allocation).

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    ^
    If it's a one dim. array aren't you supposed to do calloc? I thought that malloc was for 2d arrays :O

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Malloc can be used for any dimension of arrays, calloc can be used to but calloc is used a bit differently and initializes memory to 0.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by camel-man View Post
    The only way to determine array size based off user input is through malloc(dynamic memory allocation).
    Or use C99.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I am unfamiliar with both malloc and c99. For malloc, would i declare it at the beginning of the function or after the printf prompt. Would it be written as mallon[grades]?

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I suggest reading up on malloc a little, it involves using pointers, so if you haven't learned pointers yet now is a good time

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Code:
    int main()
    {
    #define NUMELS
       
      double grades;
      double average, sum;
      printf("Enter the grades, when finished enter a negative number:  ");
      scanf("%f", &grades[]);
       
      average = findAvg(grades[], int);
      sum = findSum(grades[], int);
    1. #define NUMELS does not define anything. It takes up space in the symbol table, but has no value associated with it.
    2. #define NUMELS should be placed globally, before your main.
    3. The function calls for finds are passing a data type, but not a variable. On the receiving end, they are receiving a data type, but not a variable.

    Another way to track array size would be to loop through the input process and increment a counter for the number of valid inputs:

    Code:
    counter = 0;
    printf("Enter the grades, when finished enter a negative number:  ");
    do
    {  
      scanf("%f", gradeinput);
      grades[counter++] = gradeinput;
    } while( gradeinput > -1 );
    The -1 will be placed in the array and you now have two means of terminating a loop that processes the elements: checking for -1 or passing the array size. However, I would spend time determining if you want to consider the -1 as adding to the array size. My philosophy is that I would treat it similar to strings( the terminating NUL is considered part of the string in some ops).
    Last edited by Cynic; 04-01-2012 at 11:39 PM. Reason: adding more info

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Well I looked up malloc like you said a few examples and a tutorial. I have been famaliarized with pointers. Only issue is on malloc I only saw what to do with an amount of memory you need to allocate.

    From what I looked at it was to be used:

    int *grades = NULL
    grades = malloc(sizeofmemory)

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    To clear that up. I only saw what do when you know what the size of the memory you need to allocate is (i.e. larger arrays)

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Right so now you can get a size you want from the user and inside the malloc instead of size of memory you can say something like grades=malloc(sizeof(number_chosen_by_user*int));

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    The only issue with that is the program is supposed to end user input by entering a negative number, not a predetermined user amount

  13. #13
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    What do you mean/? If you mean that anytime the user enters a grade it increases the array, then you will need to use a vector. Where you copy over the original previous array to a new array that is increase by one int or double whichever data type you are using.
    Last edited by camel-man; 04-01-2012 at 11:49 PM.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    The program is supposed to allow the user to enter grades, when they have no more grades to enter they are enter a negative to number to allow the prompt to end and the program to go and calculate the sum, average, letter grade, and * for below average grade. The professor is pretty strict when it comes to grading on the exact wordage of the assignment.
    If I used a user input number for memory allocation then the negative number would not be necessary.

  15. #15
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You could just declare a static array of good proportion such as int grade[100]; and then have them enter in until negative then just calculate the entered in values you do not have to calculate the entire 100. Although I dont know if your professor will approve of that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM