GPA Calculator in C / single dim array required

Segmentation Fault (core dump) error

Page 1 of 1

6 Replies - 3628 Views - Last Post: 23 November 2010 - 09:46 PM Rate Topic: -----

#1 Roobles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 10

GPA Calculator in C / single dim array required

Posted 22 November 2010 - 11:14 PM

Hello, just found your site. First semester programming. Yes, it's a homework assignment. Need assistance with segmentation fault (core dump) error. Instructor has not given much info on GNU debugger so this hasn't helped me much. Instructor advised previously that seg fault/core dump error many times means there is an invisible character on screen. Told me to re-key the code in Komodo. Have done so. Same error message. Occurs right after entry of first GPA. Have attempted to browse other postings for seg fault/core dump erros as well as assistance using GNU Debugger. Still feeling rather lost. Any information pointing in me in the right direction would be greatly appreciated.

/* GPA Calculator */

/* Libraries */
    #include<stdio.h>    

/* Main Function */
main()
{
/* Initialize and Declare Array and Variables */    
    float ArrayA[30] = {0};
    float fSum = 0;
    float fAve = 0;
    char cResponse = '\0';
    int x = 0;
    int z = 0;
    
/* Print Header and User Directions */
    printf("WELCOME TO THE GPA CALCULATOR\n");
    printf("The program calculates a class GPA.\n");
    printf("The class GPA may be requested after each GPA.\n");
    printf("Up to a maximum of 30 student scores may be entered.\n");
    
/* User Enters Student GPAs and May Request Average Class GPA */
    do {
        printf("\nEnter a GPA: \n");
        scanf("%f", ArrayA[x]);
        x++;
        printf("\nCalculate the average of all GPA's entered (Y/N)?\n");
        scanf("%c", &cResponse);
    } while (x < 30 && cResponse == 'N' || cResponse =='n');
    
/* Average GPA for Class is Calculated and Printed */
    for (z = 0; z < (x + 1); z++)
        fSum = (fSum + ArrayA[z]);
    
    fAve = (fSum / z);    
    printf("\nThe class average is: %f", fAve);
    
/* End Main Function */
}



Is This A Good Question/Topic? 0
  • +

Replies To: GPA Calculator in C / single dim array required

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: GPA Calculator in C / single dim array required

Posted 22 November 2010 - 11:29 PM

Quote

Instructor advised previously that seg fault/core dump error many times means there is an invisible character on screen. Told me to re-key the code in Komodo.
Well, there's not much you can do about a bad instructor. Segfaults typically indicate bad attempts at handling memory. There's a few ways to dig yourself into this kind of hole, but it's easily done with array access or pointers.

In particular, look at: scanf("%f", ArrayA[x]); Remember that scanf wants pointers. In this case, you want to read a float, so you must pass a pointer to a float. However, ArrayA[x] gets you a float. You want &ArrayA[x] .

You've also made a mistake later on, but I'll leave it to you to discover it.

By the way, what compiler are you using? You said Komodo, which is the editor, but what is the compiler? I ask because a modern compiler like gcc will point out this mistake. Your code also doesn't strictly conform with ISO C89 (main must return int). You aren't getting your money's worth from this instructor.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: GPA Calculator in C / single dim array required

Posted 23 November 2010 - 04:34 AM

Quote

Instructor advised previously that seg fault/core dump error many times means there is an invisible character on screen.

Shaking my head in disbelief...
Was This Post Helpful? 2
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1629
  • View blog
  • Posts: 3,092
  • Joined: 30-May 10

Re: GPA Calculator in C / single dim array required

Posted 23 November 2010 - 10:48 AM

LOL @ instructor - yeah, "invisible" like the knowledge between the left and right ears ;)

Anyway, if you're using gcc, then add some compile flags to help you spot problems before you run.
$ gcc -W -Wall -ansi -pedantic -O2 -g foo.c
foo.c:8: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:26: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
foo.c:30: warning: suggest parentheses around ‘&&’ within ‘||’
foo.c:26: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
foo.c:29: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
foo.c:40: warning: control reaches end of non-void function



Here's an example GDB session on your program.

$ gdb ./a.out
GNU gdb (GDB) 7.1-ubuntu
Copyright © 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/sc/work/a.out...done.
(gdb) run
Starting program: /home/sc/work/a.out
WELCOME TO THE GPA CALCULATOR
The program calculates a class GPA.
The class GPA may be requested after each GPA.
Up to a maximum of 30 student scores may be entered.

Enter a GPA:
3.4

Program received signal SIGSEGV, Segmentation fault.
0x00178317 in _IO_vfscanf_internal (s=Cannot access memory at address 0x40000000
) at vfscanf.c:2305
2305 vfscanf.c: No such file or directory.
in vfscanf.c
(gdb) where
#0 0x00178317 in _IO_vfscanf_internal (s=Cannot access memory at address 0x40000000
) at vfscanf.c:2305
#1 0x0017b6e8 in __scanf (format=0x804874e "%f") at scanf.c:35
#2 0x080484fe in main () at foo.c:26
(gdb) list foo.c:26
21 printf("Up to a maximum of 30 student scores may be entered.\n");
22
23 /* User Enters Student GPAs and May Request Average Class GPA */
24 do {
25 printf("\nEnter a GPA: \n");
26 scanf("%f", ArrayA[x]);
27 x++;
28 printf("\nCalculate the average of all GPA's entered (Y/N)?\n");
29 scanf("%c", &cResponse);
30 } while (x < 30 && cResponse == 'N' || cResponse =='n');

Just load the code and run it.
When it crashes, use the where command to show you how you got to that point. The most immediate point of interest is the first file/function down the list which is in your code. Look at that to see what could have caused grief for the called functions above it.

In this case, it relates directly to the "format" error message from the compiler above.
Was This Post Helpful? 3
  • +
  • -

#5 Roobles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 10

Re: GPA Calculator in C / single dim array required

Posted 23 November 2010 - 06:46 PM

Quote

You want &ArrayA[x]
[quote name='Oler1s' date='22 November 2010 - 10:29 PM' timestamp='1290493776' post='1175260'] Thank you very much for your quick and helpful reply. What a simple error! I can't believe I did that (twice, no less...) And that I didn't see it.


Quote

By the way, what compiler are you using? You said Komodo, which is the editor, but what is the compiler? I ask because a modern compiler like gcc will point out this mistake.
The shortcut is named Cygwin which appears to be gcc. We've been given very limited information on using it--basically how to install and a quick run through on trouble shooting 'Hello, World' using break, continue, display, run and quit.

Quote

Your code also doesn't strictly conform with ISO C89 (main must return int).

I didn't realize there was such a standard. Very helpful to know. I'd like to learn how to program well, not code garbage. Being as new as I am it is hard to judge when someone has coded something well (of course, I realize everyone has different styles, but I would assume that there are some ways better and easier than others....)

Quote

You aren't getting your money's worth from this instructor.
Yes, the lack of instrution and assistance has been both frustrating and disappointing. Surely a better foundation could be made instead of leaving us fairly in the dark and floundering. I am investigating other venues for learning and hope to come up with a better option for next semester.

Thank you, again! I greatly appreciate your time and input. :)
Was This Post Helpful? 0
  • +
  • -

#6 Roobles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 10

Re: GPA Calculator in C / single dim array required

Posted 23 November 2010 - 09:29 PM

Hi Salem_C, Thanks also for your time and answer. I understand the concepts of what you have written, but am still quite new and learning the specifics so the debugger isn't as useful for me as it should be. Using it is not very easy or clear for me at this point and I need/will take more time to learn more. There is no doubt that my instructor could have given us better guidance for gcc and gdb since this is a beginning class. I am grateful to those of you willing to share your knowledge with us new folks!
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: GPA Calculator in C / single dim array required

Posted 23 November 2010 - 09:46 PM

Quote

The shortcut is named Cygwin which appears to be gcc. We've been given very limited information on using it--basically how to install and a quick run through on trouble shooting 'Hello, World' using break, continue, display, run and quit.
Ouch. Generally, people do not want to use Cygwin. (If you need Cygwin, you know you need it.) Beginners definitely shouldn't be going through Cygwin.

At least your compiler is gcc. Salem's advice is all very valid. The flags I use are something like -ansi, -pedantic, -Wall, -Wextra, -Wformat=2, -Wshadow, with variations as appropriate.

By the way, the compilers I use on Windows are MSVC (Google Visual C++ Express or since you are a student, "Dreamspark" to get Visual Studio for free) and MinGW port of gcc (Google TDM-GCC). The MSVC debugger is much easier and nicer to work with, so if you have free time, you may want to experiment.

Quote

I am investigating other venues for learning and hope to come up with a better option for next semester.
I tell people not to start out with C and C++. Getting burned on bad instruction is one of the reasons why. A good avenue is to start with a high level language like Python or Ruby and use that to acquire the necessary foundation. That foundation makes learning C significantly easier.

If you want to learn to program well, your university courses are unlikely to provide the necessary environment. It's true for everyone. Your best bet is to get through the language courses as quickly as possible and focus on picking theory classes. Learn the tools on your own, use courses to learn the theory.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1