I still pretty new to programming and my only prior experience before C was Javascript. I'm doing the CS50 Introduction to Computer Science and in one of the lectures there's an example code that computes the average of some user input. It looks like this:
#include <cs50.h>
#include <stdio.h>
const int TOTAL = 3;
float average(int length, int array[])
int main(void)
{
int scores[TOTAL];
for (int i = 0; i < TOTAL; i++)
{
scores[i] = get_int("Score: ");
}
printf("Average: %f\n", average(TOTAL, scores);
}
float average(int length, int array[])
{
int sum = 0;
for (int i = 0; i < length; i++)
{
sum += array[i];
}
return sum / (float) length;
}
The feature that I'm trying to add is to dynamically store the size of the array depending of the user input, instead of having one variable (TOTAL in this case). For example: I need to have a loop that is always asking the user for a score (instead of just 3 times like the code above), and when the user types zero(0), the loops breaks and the size of the array is defined by how many times the user has typed some score.
This is what I've done:
int main(void)
{
int score;
// start count for size of array
int count = - 1;
do
{
score = get_int("Score: ");
// add one to the count for each score
count++;
}
while (score != 0);
// now the size of the array is defined by how many times the user has typed.
int scores[count];
for (int i = 0; i < count; i++)
{
// how do I add each score to the array???
}
}
My problem is how to add each score that the user types to the array. In advance thanks!!!
regarding:
int main(void)
{
int score;
// start count for size of array
int count = - 1;
do
{
score = get_int("Score: ");
// add one to the count for each score
count++;
}
while (score != 0);
// now the size of the array is defined by how many times the user has typed.
int scores[count];
for (int i = 0; i < count; i++)
{
// how do I add each score to the array???
}
}
this does not compile and contains several logic errors
it is missing the statements: #include <cs50.h>
and
#include <stdio.h>
regarding:
int score;
// start count for size of array
int count = - 1;
do
{
score = get_int("Score: ");
// add one to the count for each score
count++;
}
while (score != 0);
This only defines a single variable: score
and each time through the loop that single variable is overlayed. Also, the first time through the loop, the counter: count
will be incremented to 0, not 1
on each following time through the loop, the variable score
will be overlayed (I.E. all prior values entered by the user will be lost)
suggest using dynamic memory. Note: to use dynamic memory, will need the header file: stdlib.h
for the prototypes: malloc()
and free()
. Suggest:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
// pointer to array of scores
int * score = NULL;
// start count for size of array
int count = 0;
while( 1 )
{
int score = get_int("Score: ");
if( score != 0 )
{ // then user has entered another score to put into array
count++;
int * temp = realloc( scores, count * sizeof( int ) )
if( ! temp )
{ // realloc failed
// output error info to `stderr`
// note: `perror()` from `stdio.h`
perror( "realloc failed" );
// cleanup
free( scores );
// `exit()` and `EXIT_FAILURE` from `stdlib.h`
exit( EXIT_FAILURE );
}
// implied else, 'realloc()' successful, so update the target pointer
scores = temp;
// insert the new score into the array
scores[ count ] = score;
}
else
{ // user entered 0 so exit the loop
break;
}
}
note: before exiting the program, pass scores
to free()
so no memory leak.