Thread: im a newbie at c please help.

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    3

    im a newbie at c please help.

    Please could someone explain to me why this isnt working, also is there a way for me to not have to type in all the variables when i want multiple typed like i have done here. thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
     
     int grade[10][0];
     
     grade[0][0]=0;
     grade[1][0]=0;
     grade[2][0]=0;
     grade[3][0]=0;
     grade[4][0]=0;
     grade[5][0]=0;
     grade[6][0]=0;
     grade[7][0]=0;
     grade[8][0]=0;
     grade[9][0]=0;
     printf("Please enter your students exam mark seperated by a space \n");
     fflush(stdin);
     scanf("%d %d %d %d %d %d %d %d %d %d \n", grade[1][0], grade[2][0], grade[3][0],grade[4][0], grade[5][0], grade[6][0], grade[7][0], grade[8][0], grade[9][0], grade[0][0]);
     float totgrade= grade[1][0]+grade[2][0]+grade[3][0]+grade[4][0]+grade[5][0]+grade[6][0]+grade[7][0]+grade[8][0]+grade[9][0]+grade[10][0];
     
     printf("The total mark is: %f \n", totgrade);
     
     float avgrade=totgrade/10;
     printf("The average mark = %.1f", avgrade);
     
     system("pause");
     return 0; 
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This doesn't make sense and should have resulted in a compile error, or at least a warning:
    Code:
    int grade[10][0];
    It looks like you want to read in the grades of ten students, one grade per student, so you should have written:
    Code:
    int grade[10] = {0};
    The zero initialisation means that you don't have to manually set the initial value of all the elements to zero.

    This is non-standard and you should remove it since you don't need it here:
    Code:
    fflush(stdin);
    Next, for the scanf, while you could have a format string consisting of ten format specifiers, you may find it easier to use a loop that reads one grade per iteration. Note that the elements of the grade array start from grade[0] and ends at grade[9], not grade[10].

    Oh, and remember to indent your code so as to make it more readable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    3

    Ive got to this but it still crashes after i input the 10 numbers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
     
     int grade[10]={0};
      printf("Please enter your students exam mark seperated by a space \n");
      scanf("%d %d %d %d %d %d %d %d %d %d \n", grade[1], grade[2], grade[3],grade[4], grade[5], grade[6], grade[7], grade[8], grade[9], grade[0]);
      printf("%d", grade[4]);
     
     float totgrade= grade[1]+grade[2]+grade[3]+grade[4]+grade[5]+grade[6]+grade[7]+grade[8]+grade[9]+grade[0];
     
      printf("The total mark is: %f \n", totgrade);
     
     float avgrade=totgrade/10;
      printf("The average mark = %.1f", avgrade);
     
     system("pause");
     return 0; 
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Every grade in your scanf line needs to be &grade[n]

    A better compiler will tell you such things.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
       scanf("%d %d %d %d %d %d %d %d %d %d \n", grade[1], grade[2], grade[3],grade[4], grade[5], grade[6], grade[7], grade[8], grade[9], grade[0]);
             ^
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 6 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 7 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 8 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 9 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 10 has type ‘int’ [-Wformat=]
    foo.c:7:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 11 has type ‘int’ [-Wformat=]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello, I am a newbie and so I have a newbie question;)
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 12-20-2012, 10:59 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  4. Newbie Help
    By fusoya77 in forum Game Programming
    Replies: 2
    Last Post: 01-11-2003, 08:18 PM
  5. newbie
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 05-26-2002, 01:47 PM

Tags for this Thread