#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]); } }
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

24 replies to this topic
#1
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.
#2
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?
The roots of education are bitter, but the fruit is sweet.
#3
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.
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#4
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!
#5
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; }
#6
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)
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:
#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.
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#7
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!
#8
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.
#9
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.
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#10
Posted 17 November 2010 - 05:36 AM
This will leave last row and column undefined. You probably meant < 200 or <= 199.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"); }
The roots of education are bitter, but the fruit is sweet.
#11
Posted 17 November 2010 - 05:38 AM
I did a quick edit of his code, missed that one.
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#12
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.
Also tagged with one or more of these keywords: array, printing
Language Forums →
C# →
Counting the integers frequences in an array entered by the userStarted by AndreMarques, 27 Nov 2017 ![]() |
|
![]() |
||
Language Forums →
PHP →
How Come Variable Not Assigned And Php Knows Where To Look For Value ?Started by uniqueideaman, 06 May 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
help me . c++. ArraysStarted by girl2383, 06 Feb 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
Selecting an element on a 2D arrayStarted by LonelyGuySylar, 18 May 2016 ![]() |
|
![]() |
||
Language Forums →
Java →
Include a specific error, task, problem, or question in your titleStarted by rjb7, 15 Apr 2016 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download