Like Tree1Likes
  • 1 Post By AndiPersti

Some error, dunno what

This is a discussion on Some error, dunno what within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int marks[50],i,n,sum=0; float avg; printf("ENTER THE NUMBER OF STDENTS(should not exceed 50): \n"); scanf("%d",&n); ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    Some error, dunno what

    Code:
    #include <stdio.h>
    int main()
    {
      int marks[50],i,n,sum=0;
      float avg;
      printf("ENTER THE NUMBER OF STDENTS(should not exceed 50): \n");
      scanf("%d",&n);
      for(i=0;i<=n;i++)
      {
      printf("STUDENT NUMBER %d SCORED:\n",i+1);
      scanf("%d",marks[i]);
      sum+=marks[i];
      }
      avg=sum/n;
      printf("THE AVERAGE MARKS OF STUDENTS ARE %f",avg);
      return 0;
    }


    plzz tell me whats wrong with this program... every time i try 2 run this errors occur...

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by suvojit biswas View Post
    plzz tell me whats wrong with this program... every time i try 2 run this errors occur...
    Could you please also copy'n'paste the error? I still haven't met any mind reader here.
    And please use normal English (no "plzz" and "2" instead of "to").

    Code:
    for(i=0;i<=n;i++)
    {
      printf("STUDENT NUMBER %d SCORED:\n",i+1);
      scanf("%d",marks[i]);
    If n == 50 (or bigger) then "i" will get up to 50 too and marks[i] == marks[50] is undefined behavior.
    I.e. your "<=" should be "<".

    Edit: And scanf("%d") expects a pointer to int but marks[i] is an int.

    Code:
    avg=sum/n;
    "sum" is an int, "n" is an int, thus the result of "sum / n" will be an int too and then it will be cast to a float.

    Bye, Andreas
    Last edited by AndiPersti; 10-04-2012 at 09:38 AM. Reason: wrong argument to scanf
    Matticus likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't run, dunno what is the problem
    By jian in forum C Programming
    Replies: 2
    Last Post: 12-25-2010, 03:11 AM
  2. Replies: 7
    Last Post: 08-13-2010, 12:24 AM
  3. I dunno, kinda cool
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 07-03-2003, 09:45 PM
  4. i dunno how to do loops and functions together
    By Noobie in forum C++ Programming
    Replies: 30
    Last Post: 02-03-2003, 05:05 PM
  5. I now have the DX SDK (and I dunno how to.......)
    By Stealth in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2001, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21