d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C And Arrays > Omfg Seg Faultz!~!~!~!
Add Reply New Topic New Poll
Member
Posts: 102
Joined: May 30 2012
Gold: 0.00
Jun 10 2012 05:45pm
Just made a program for an intro to C class and got a Segmentation fault. Searched and I guess i'm oblivious to what the hell i'm doing wrong.
Hoping someone could point me to my major fuck up.
All the program is meant to do is ask user for amount of different types of assignments, take in percentages for that many of a certain assignment, add them up, calculate an average, multiply that average by how much that respected assignment is worth, then add up all averages for a final average grade. Pointless program...I know....
Thanks

Here is meh code:
Code
#include <stdio.h>

int main(){
       int numq, numa, numl, numt;
       char final;
       int i;
       int j;
       int k;
       int l;
       float quiz_grade[numq];
       float assignment_grade[numa];
       float lab_grade[numl];
       float test_grade[numt];
       float final_grade;
       float total_assignment;
       float total_quiz;
       float total_lab;
       float total_test;
       float final_quiz, final_final, final_lab, final_assignment, final_test;
       float final_avgwfinal;
       float final_avgwofinal;
       float avg_quiz, avg_lab, avg_assignment, avg_test;
       printf("How many quizes do you wish to calculate for?\n");
       scanf("%lf", &numq);
       printf("How many assignments do you wish to calculate for?\n");
       scanf("%lf", &numa);
       printf("How many labs do you wish to calculate for?\n");
       scanf("%lf", &numl);
       printf("How many tests do you wish to calculate for?(NOT INCLUDING FINAL)\n");
       scanf("%lf", &numt);
       getchar();
       printf("Do you wish to calculate for the final?(y=yes, n=no)");
       scanf("%c", &final);
       for(i = 0;i < numq;i++){
               printf("Enter the quiz grade(in percentage) for quiz %d: ", (i + 1));
               scanf("%lf", &quiz_grade[i]);
               total_quiz += quiz_grade[i];
       }
       for(j = 0;j < numa;j++){
               printf("Enter the assignment grade(in percentage) for assignment %d: ", (j + 1));
               scanf("%lf", &assignment_grade[j]);
               total_assignment += assignment_grade[j];
       }
       for(k = 0;k < numl;k++){
               printf("Enter the lab grade(in percentage) for lab %d: ", (k + 1));
               scanf("%lf", &lab_grade[k]);
               total_lab += lab_grade[k];
       }
       for(l = 0;l < numt;l++) {
               printf("Enter the test grade(in percentage) for test %d: ", (l + 1));
               scanf("%lf", &test_grade[l]);
               total_test += test_grade[l];
       }
       if(final =='y'){
               printf("Enter the grade(in percentrage) for the final you wished to calculate for: ");
               scanf("%lf", &final_grade);
       }
  avg_quiz = (total_quiz / numq);
       printf("Your average grade on quizes is %f", avg_quiz);
       avg_assignment = (total_assignment / numa);
       printf("Your average grade on assignments is %f", avg_assignment);
       avg_lab = (total_lab / numl);
       printf("Your average grade on labs is %f", avg_lab);
       avg_test = (total_test / numt);
       printf("Your average grade on tests is %f", avg_test);

       final_quiz = (avg_quiz * .1);
       final_lab = (avg_lab * .1);
       final_assignment = (avg_assignment * .4);
       final_test = (avg_test * .25);
       final_final = (final_grade * .15);
       final_avgwfinal = (final_quiz + final_lab + final_assignment + final_test + final_final);
       final_avgwofinal = (final_quiz + final_lab + final_assignment + final_test);
       if(final == 'y') {
               printf("Your final average grade(including the final) is: %lf", final_avgwfinal);
       }
       if(final == 'n') {
               printf("Your final average grade excluding the final is: %lf", final_avgwofinal);
       }

       return 0;
}

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 10 2012 06:00pm
I'm not big on C, but this doesn't look correct

int numq, numa, numl, numt;
float quiz_grade[numq];
float assignment_grade[numa];
float lab_grade[numl];
float test_grade[numt];

if your local int variables are 0 by default, then i guess your arrays all have 0 length. if your local int vars are not 0 by default, then it's a random size?

you cannot resize an array in C because they're in consecutive memory blocks. they keep whatever size they have.

This post was edited by carteblanche on Jun 10 2012 06:02pm
Member
Posts: 4,250
Joined: Apr 17 2005
Gold: 0.01
Jun 10 2012 06:18pm
array size must be constant value;

you input uninitialized variables as array sizes

explanation of your error:

http://www.linuxquestions.org/questions/programming-9/c-segmentation-fault-156985/

edit:

try to switch 1st line into this:

const int numq = 10, numa = 10, numl = 10, numt = 10;

and remove input values

it compiles now

This post was edited by Elles on Jun 10 2012 06:42pm
Member
Posts: 102
Joined: May 30 2012
Gold: 0.00
Jun 10 2012 06:51pm
Quote (Elles @ Jun 10 2012 05:18pm)

+

it of course looks like you didnt code this



Ouch,
but thanks for the help.
amateur mistake.
I am seeming to have a new problem though, no matter what i enter for numq or any of my num variables it asks for the grade percentages 24 times.
Any ideas?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 10 2012 10:14pm
Quote (Oomps @ Jun 10 2012 08:51pm)
Ouch,
but thanks for the help.
amateur mistake.
I am seeming to have a new problem though, no matter what i enter for numq or any of my num variables it asks for the grade percentages 24 times.
Any ideas?


this is the time to pick of a debugger and learn how to use it lol. depending on your platform and setup you can do this in many ways.

if using code:blocks on windows it already has a debugger built in.

if using plain gcc on linux you can compile it with the -g flag and open it in gdb.

at which point you would want to type:

break main

which would set a breakpoint at the label "main" then type

r

to make the application run. then you can type

s

to single step through each line of your applications. and then your can use the

p varname

command to check what the contents of your variables are.

if you are on windows find out on your own lol.
Member
Posts: 102
Joined: May 30 2012
Gold: 0.00
Jun 11 2012 01:55am
Quote (AbDuCt @ Jun 10 2012 09:14pm)
this is the time to pick of a debugger and learn how to use it lol. depending on your platform and setup you can do this in many ways.

if using code:blocks on windows it already has a debugger built in.

if using plain gcc on linux you can compile it with the -g flag and open it in gdb.

at which point you would want to type:

break main

which would set a breakpoint at the label "main" then type

r

to make the application run. then you can type

s

to single step through each line of your applications. and then your can use the

p varname

command to check what the contents of your variables are.

if you are on windows find out on your own lol.


Thanks a BUNCH(Especially for explaining it on a retard's level)!

This post was edited by Oomps on Jun 11 2012 01:55am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll