Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Array not printing

array printing

  • Please log in to reply
24 replies to this topic

#13 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2010 - 05:48 AM

I had updated your code to reflect my mistake spotted by FlyingDutchman, and also your problem:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#include "constants.h"

int main(void)
{

  srand(time(NULL));

  int scores [NUMBERofSTUDENTS][EXAMSCORES];
  int i, j;
  float exam1_sum;
  float exam1_average;
  float class_sum;
  float class_average;

  for(i = 0; i <= NUMBERofSTUDENTS - 1; i++) {
    for(j = 0; j <= EXAMSCORES - 1; j++) {
        scores[ i ][ j ] = (rand() % 100 + 1);
        class_sum += scores[ i ][ j ];
    }
    class_average = class_sum / 800;
  }


  for(i = 0; i <= NUMBERofSTUDENTS - 1; i++){
    for(j = 0; j <= EXAMSCORES - 1; j++){
     printf("%d ", scores[ i ][ j ]);
    }
    printf("\n");
  }
  
  for(i = 0; i <= NUMBERofSTUDENTS - 1; i++){
    for(j = 0; j <= EXAMSCORES - 1; j++){
        exam1_sum += scores[ i ][ j ];
    }
    exam1_average = exam1_sum / 800;
  }


            printf("Exam 1 average: %f\n", exam1_average);
            printf("Class average: %f\n", class_average);


  return 0;

}
On your final loop you wrote j <= 1 instead of j <= EXAMSCORES - 1 so your class average is off by a lot.

Your other problem was you were calculating the averages 200 times in each loop, it should be placed out of the loop. (I fixed that too)

EDIT: Fixed the third problem, the first loop you missed the "- 1". They both work now.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#14 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 05:56 AM

Ok thanks! Also to find the exam 1 average, could I just replace EXAMSCORES -1 with 0 to find only the scores for exam 1? For example could I do this:


 for(i = 0; i <= NUMBERofSTUDENTS - 1; i++){
    for(j = 0; j <= 0; j++){
        exam1_sum += scores[ i ][ j ];
    }
    exam1_average = exam1_sum / 200;
  }

  • 0

#15 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2010 - 06:02 AM

Yes, I get 49.95% which is correct. Actually skipping the loop would make more sense, for example setting 'j' to 0-3 to access exams 1-4.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#16 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 06:08 AM

Hmmmm it keeps giving me a big number for the Exam 1 average, I am not sure why the only thing I changed was the code I posted above.
  • 0

#17 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2010 - 06:11 AM

What number do you get? It could be you are running the loop twice.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#18 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 06:14 AM

59163366811698218000000000000.00000
LOL I seriously don't know what its doing that for.
  • 0

#19 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2010 - 06:19 AM

:) .. Try to compare it with mine, it works:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#include "constants.h"

int main(void)
{

  srand(time(NULL));

  int scores [NUMBERofSTUDENTS][EXAMSCORES];
  int i, j;
  float exam1_sum;
  float exam1_average;
  float class_sum;
  float class_average;

  for(i = 0; i <= NUMBERofSTUDENTS - 1; i++) {
    for(j = 0; j <= EXAMSCORES - 1; j++) {
        scores[ i ][ j ] = (rand() % 100 + 1);
        class_sum += scores[ i ][ j ];
    }
  }
  class_average = class_sum / 800;

  for(i = 0; i <= NUMBERofSTUDENTS - 1; i++){
    for(j = 0; j <= EXAMSCORES - 1; j++){
     printf("%d ", scores[ i ][ j ]);
    }
    printf("\n");
  }
  
  for(i = 0; i <= NUMBERofSTUDENTS - 1; i++){
    exam1_sum += scores[ i ][ 0 ];
  }
  exam1_average = exam1_sum / 200;


            printf("Exam 1 average: %f\n", exam1_average);
            printf("Class average: %f\n", class_average);


  return 0;

}

EDIT: Another few fixes, brought average calculation out of another loop, forgot to do that. Also got rid of the useless loop in exam1 average calculation. I edit ALOT when it's late.

Edited by Alexander, 17 November 2010 - 06:25 AM.
Trashed loop for static number in exam1_average

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#20 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 06:25 AM

LOL it still is not working for me, I even just went ahead and tried to copy and past the code you posted, and it would not work. I looked through, it makes complete sense and should work. I am baffled at the moment. Hmmm could it be my compiler?
  • 0

#21 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2010 - 06:29 AM

I got it.. Initialize all your variables up top to 0. We are performing division on the _sum variables although it is not initialized yet.

Learned a lesson there!
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#22 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 06:33 AM

Yep, that fixed it. Thanks for the help! Coding is all about the **** little things haha
  • 0

#23 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 17 November 2010 - 06:36 AM

No problem, I am glad I was able to debug at 6:30 in the morning after no sleep. :)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#24 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 06:39 AM

Me too, haha I would have been pounding my face into the screen wondering why in the world this 2d array was not wanting to cooperate lol.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download