1. ## 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 average, sum;
printf("Enter the grades, when finished enter a negative number:  ");

printf("The sum of the grades is %f", sum);
printf("The average of the grades is %f", average);
return 0;
}
{
int i;
double sumnums = 0.0;
for (i = 0; i < NUMELS; i++)
return (sumnums/NUMELS); /*calculate and return average*/
}
{
int i;
double sumnums = 0.0;
for (i = 0; i < NUMELS; i++)
return (sumnums); /*reutrn the sum*/
}

2. The only way to determine array size based off user input is through malloc(dynamic memory allocation).

3. ^
If it's a one dim. array aren't you supposed to do calloc? I thought that malloc was for 2d arrays :O

4. 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. Originally Posted by camel-man
The only way to determine array size based off user input is through malloc(dynamic memory allocation).
Or use C99.

6. 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. 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. Code:
int main()
{
#define NUMELS

double average, sum;
printf("Enter the grades, when finished enter a negative number:  ");

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
{
} 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).

9. 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:

10. 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. 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. 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. 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.

14. 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. 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.