#Trouble storing GPA float variables in an array

10 messages · Page 1 of 1 (latest)

rose kelp
#

Hello all, I'm doing a C challenge where the intent is to get an average GPA from 10 GPA inputs. I am still a noob, only about two weeks into C programming so this could be a simple problem I'm running into. Essentially my float inputs are saving as 0.0 instead on inputted user values. Does anyone have any suggestions and further more an explanation on why this is happening to me? I've removed the math of getting the GPA average because I need to solve this problem first, so that will not be in the code. the code is below.

#include <stdio.h>

//Function prototypes
char cBreak(void);

//start main func
main(){

float iArray[20];
int iIndexPos = 0;
float fGPA = 0;
int iTotalGPA = 0;

cBreak();
printf("\nGPA Average Calculator\n");

printf("\nDescription:");
printf("\nUsing this program, you can calculate an average GPA of up to 10 inputs.");
cBreak();


for(iIndexPos = 0; iIndexPos <= 9; iIndexPos++){//start for, keep track of index position to set element values
    if(iIndexPos == 0){
        printf("\nPlease enter your %dst GPA:", iIndexPos + 1);
        scanf(" %.2f", &iArray[iIndexPos]);
    }
    else if(iIndexPos == 1){
        printf("\nPlease enter your %dnd GPA:", iIndexPos + 1);
        scanf(" %f", &iArray[iIndexPos]);
    }
    else if(iIndexPos == 2){
        printf("\nPlease enter your %drd GPA:", iIndexPos + 1);
        scanf(" %f", &iArray[iIndexPos]);
    }
    else if(iIndexPos >= 3){
        printf("\nPlease enter your %dth GPA:", iIndexPos + 1);
        scanf(" %f", &iArray[iIndexPos]);
    }    
}// end for

for(iIndexPos = 0; iIndexPos <= 9; iIndexPos++){//used to find out how many gpas to average
    if(iArray[iIndexPos] > 0.1){
        iTotalGPA++;
    }
}


for(iIndexPos = 0; iIndexPos <= 9; iIndexPos++){
    printf("%.1f", fGPA);
}

}

//function definition 
char cBreak(void){
 printf("\n_________________________________________________________________________\n");

}```
rapid bearBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

digital trail
#

putting " .2 inscanf's format string doesn't do what you think it does. get rid of it.
as a sidenote - scanf return some value. you should check it

#

other notable side notes:

main() { ... }```
is quite an old signature. one should use `int main(void) { ... }` (up to c23) or `int main() { ... }` (c23 onwards)

that `if...else` tree within your loop is redundant

inputting 4 elements (`0..3`) while reading 9 is undefined behavior. the other 5 elements are uninitialized

declaring `cBreak` as a 'function taking `void` elements returning a `char`' yet failing to return anything, while isn't wrong - isn't a wise thing to do
rose kelp
#

I am reading a pretty old book, so some of the things I'm doing might be out dated. that explains why I always get a warning for my main function lol.

For the else if tree, I was using that to determine the postfix for the number, like "st, nd, rd, th" I.E 1st, 2nd, 3rd, 4th. Do you have any suggestions or a direction to take to make it less redundant?

I'll have to look into undefined behavior and how to mitigate/fix that note.

as for cBreak, I thought it was returning the break line "_____". I'll look into that also.

i'll remove the .2 and see where that leaves me also, I will let you know in a few

digital trail
#

one can also create a small lookup table thingy and use that instead

#

as for cBreak, I thought it was returning the break line "_". I'll look into that also.
its not. its printing something. aside from main, if a function doesn't contain a return statement - its either:

  • doesn't return anything (if the function declared as void)
  • the return value is unspecified (for all functions other than main)
rose kelp
#

Ok, obviously I need to take another look at functions to understand them a bit better. I kind of rushed that chapter lol. I'm currently on the array chapter and that why I'm doing this now. I got the floats to save correctly so that good.

Thanks for your input and ideas, I'll be taking a look into what we talked about after this chapter.

#

!solved