How do you assign user input to be stored into arrays
My challenge is to write a program that will store a user's GPA as a float value in a array. The user can enter up to 30 GPA's and display the average if wanted, after each value is iuput. What I'm confused about is how to use a scanf()
function while in a loop to store float values in an array. Can anyone explain with a sample code?
OK so this is the code that I have started. I know it's bogus but I wanted everyone to get a better idea of the program I'm trying to write. I want the user to type in how many GPA's they have. The number they input will be the number of arrays I will have.
#include <stdio.h>
main()
{
int loopcount = 0;
int NumGpa = 0;
NumGpa = 0 - 1;
float = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25;
开发者_运维百科 int gpa[NumGpa] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int i = 0;
int total =0;
printf("\t\tWelcome to the GPA calculator.\n ");
printf("Please enter the number of classes you have counted for you GPA: ");
scanf("%d", &NumGpa);
while (loopcount < NumGpa)
{
printf("\nenter your first GPA: ");
scanf("%d", &1); //??????????????(i want loops to use scanf() if order &2,&3 ect?
for (i=0; i<gpa; i++)
{
total += arbin[NumGpa];
}
printf(" you average gpa is %d", NumGpa / ????);
getchar();
}
}
When you're reading a float
with scanf
, you pass the address of the float
in question. You can take the address of an item in an array about the same way as you would anything else, using the address-of operator (&
).
Alternatively, you can simplify things just a little bit. In C, the subscript operator ([]
) is equivalent to a pointer operation. In particular, a[b]
is equivalent to *(a+b)
. However, &
and *
are pretty much exact opposites, so in this case they'd end up cancelling each other out. As such, if you prefer you can just use a+b
, where one of them is the name of the array, and the other is the index of the item in the array that you want to read.
int i = 0; // current user
float user_gpa[NUM_USERS]; // your array
while (i < num_users) {
scanf("%f", &user_gpa[i]); // reading data for the i-th user (%f means float)
i = i + 1; // next user
}
try this
for(i=0; i<30; i++)
scanf("%f", arrayname[i]);
if you want the average directly then try this
float temp, ave=0;
int i;
for (i=1; i<=30; i++)
{
scanf("%f", &temp);
ave += temp;
}
printf("%f", ave/30);
scanf("%f",&arrayVariableName[i]);
Well, there were few many mistakes in your code, Probably this might help.
enter code here
void main()(
int loopcount = 0;
int NumGpa;
NumGpa = 0 - 1;
/*
float = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25; i don't understand why were you doing this.
int gpa[NumGpa] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
*/
int i = 0;
float total =0; /this is float so that output will be accurate.
printf("\t\tWelcome to the GPA calculator.\n");
printf("Please enter the number of classes you have counted for you GPA: ");
scanf("%d", &NumGpa);
int gpa[NumGpa];
while (loopcount < NumGpa)
{
printf("\nenter your %d GPA: ", loopcount);
scanf("%d", &gpa[loopcount]);
loopcount++;
}
for (i=0; i<NumGpa; i++)
{
total += gpa[i];
}
printf(" you average gpa is %f", total / NumGpa);
getchar();
}
精彩评论