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

#1 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 10 November 2010 - 03:55 PM

Hey all, I was doing an assignment on arrays today, but could not get this array to print. The assignment was to print 1000 arrays and insert a random value between 0 and 1,000,000. Then average them and print out the ones that were 90% below and above the average.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ACCOUNT 1000

main ()
{
  // seed the random number generator

  srand( time(NULL) );

  // size is set by the ACCOUNT constant

  int accounts[ACCOUNT];
  int i;
  int sum = 0;
  int average;
  int flag;
  int aboveFlag;
  int belowFlag;

  // loop through and insert a random value
  // calculats the average

  for ( i = 0; i < ACCOUNT; i ++ ) {
    accounts[i] = rand() % 1000000;
    sum += accounts[i];
    average = sum / 1000;
    flag = average * (.9);
    aboveFlag = average + flag;
    belowFlag = average - flag;
  }


   // determines what number is %90 over and under average


   // prints the flagged accounts

   for ( i = 0; accounts[i] <= belowFlag; i ++)  {
        printf ("Account Number: %d Balance: %d\n", i, accounts[i]);
    }

   for ( i = 0; accounts[i] >= aboveFlag; i ++) {
        printf ("Account Number: %d Balance: %d\n", i, accounts[i]);
    }

}

  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 10 November 2010 - 04:08 PM

   for ( i = 0; [B]accounts[i][/B] <= belowFlag; i ++)  {
        printf ("Account Number: %d Balance: %d\n", i, accounts[i]);
    }

   for ( i = 0; [B]accounts[i][/B] >= aboveFlag; i ++) {
        printf ("Account Number: %d Balance: %d\n", i, accounts[i]);
    }
Maybe that in bold is causing problems?
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 Alexander

Alexander

    YOL9

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

Posted 10 November 2010 - 05:22 PM

You need to change many of those types to floats, an integer rounding will skew the results without you knowing it. Just fixing your code and adding debugging information, you can run this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ACCOUNT 1000

int main ()
{
  // seed the random number generator
  srand( time(NULL) );

  // size is set by the ACCOUNT constant

  int accounts[ACCOUNT];
  int i;
  int sum = 0;
  float average;
  float flag;
  float aboveFlag;
  float belowFlag;

  // loop through and insert a random value
  // calculats the average

  for ( i = 0; i < ACCOUNT; i++ ) {
    accounts[i] = rand() % 1000000;
    sum += accounts[i];
    average = sum / 1000;
    flag = average * (.9);
    aboveFlag = average + flag;
    belowFlag = average - flag;
  }

//debugging
printf("\n\nAboveFlag: %f, BelowFlag: %f\n\n", aboveFlag, belowFlag);

   // determines what number is %90 over and under average

   // prints the flagged accounts

//proper looping
   for ( i = 0; i <= ACCOUNT; i++)  {
        if(accounts[i] <= belowFlag) {
            printf ("(below) Account Number: %d Balance: %d\n", i, accounts[i]);
        }
    }

   for ( i = 0; i <= ACCOUNT; i++) {
        if(accounts[i] >= aboveFlag) {
            printf ("(above) Account Number: %d Balance: %d\n", i, accounts[i]);
        }
    }

}
Notice your below/above flag and sum code is set each time in the loop, not outside of the loop, so it will reset and be a bit sketchy and catches a lot less.
  • 0

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


#4 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 10 November 2010 - 05:27 PM

Ahhh ok gotcha, dumb I don't know why my loops were so messed up. Well lets hope the non fixed one didn't get me an F lol that would suck all due to a silly mistake. Thanks for the help!
  • 0

#5 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 03:09 AM

OK having trouble with 2d arrays now lol. Essentially am creating a list of all the exam scores for the students. 200 students and 4 exams. I need it to give a random score between 0 to 100 for each exam and each student. I have no idea how to do this, possibly creating another .c file for this? I am not sure this is what i have so far. Any clues will be awesome or a place to go read up on this that will help will be great help!


#include <stdio.h>
#include <time.h>

#include "constants.h"

int main(void)
{

    srand(time(NULL));

  int scores [NUMBERofSTUDENTS][EXAMSCORES];
  int i, j;

  for(i = 1; i < 200; i++)
    for(j = 0; j < 4; j++)
        scores[ i ][ j ] = i * j;



  for(i = 1; i < 200; i++){
      for(j = 0; j < 4; j++)
                printf("%d ", scores[ i ][ j ]);

    printf("\n");
  }

  return 0;
}

  • 0

#6 Alexander

Alexander

    YOL9

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

Posted 17 November 2010 - 03:26 AM

You mean like this? I had changed a bit of the code (replacing constants with numbers to make this example work)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(void)
{

  srand(time(NULL));

  int scores [200][4];
  int i, j;

  for(i = 0; i <= 199; i++) {
    for(j = 0; j <= 3; j++) {
        scores[ i ][ j ] = (rand() % 100 + 1);
    }
  }

  for(i = 0; i <= 199; i++){
    for(j = 0; j <= 3; j++){
      printf("%d ", scores[ i ][ j ]);
    }
    printf("\n");
  }

  return 0;
}
You need to remember how arrays are assigned. You write array[200] for 200 elements, but you need to iterate through it with for(i=0;i<=199;i++) as 0 is element one.

You did the same for "j", your j=0;j<=4;j++ loops 5 times not 4.

In your real code you can use constants, but just get rid of one during looping:
for(i=0;i<=NUMBERofSTUDENTS - 1;i++) { ...

Edited by Alexander, 17 November 2010 - 05:39 AM.

  • 0

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


#7 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 03:30 AM

Ohhhh!!!! Wow do I feel stupid! Thanks, this definitely helps me now I can hopefully finish the rest of this thing!
  • 0

#8 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 03:53 AM

Ok second question, I applied the way you showed me how to do the averages on the first array, but it did not work on the 2d array. Is it because its a 2d array and not a single array? It just gives me abnormally large numbers.

Edited by JanitorMoe, 17 November 2010 - 05:01 AM.

  • 0

#9 Alexander

Alexander

    YOL9

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

Posted 17 November 2010 - 05:34 AM

It should be similar, although it will require you to do extra steps for each step before. What code do you have? You are most likely overrunning the array by getting large values.
  • 0

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


#10 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 17 November 2010 - 05:36 AM

  for(i = 0; i < 199; i++) {
    for(j = 0; j < 3; j++) {
        scores[ i ][ j ] = (rand() % 100 + 1);
    }
  }

  for(i = 0; i < 199; i++){
    for(j = 0; j < 3; j++){
      printf("%d ", scores[ i ][ j ]);
    }
    printf("\n");
  }

This will leave last row and column undefined. You probably meant < 200 or <= 199.
  • -1

The roots of education are bitter, but the fruit is sweet.


#11 Alexander

Alexander

    YOL9

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

Posted 17 November 2010 - 05:38 AM

I did a quick edit of his code, missed that one.
  • 0

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


#12 JanitorMoe

JanitorMoe

    CC Resident

  • Member
  • PipPipPipPip
  • 55 posts

Posted 17 November 2010 - 05:39 AM

Here is what I have so far. The class average is giving me a number which could be correct, but the exam 1 average is def wrong

#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 <= 1; j++){
        exam1_sum += scores[ i ][ j ];
                exam1_average = exam1_sum / 200;

    }

  }


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


  return 0;

}

Edited by JanitorMoe, 17 November 2010 - 05:43 AM.
fixed what dutchman pointed out.

  • 0





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