# array help

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-10-2006
bkamagnum
array help
hello im doin a project and the last part deals with array please help if u can....

If I want an array to print out for example:
grades in the range of : 0-24, 25-49, 50-74, and 75-99 i know how to if else the grades and counter them reading from an infile. How do i put that in an array that will print it out like so.

range # of students
0-24 3
25-49 4
50-74 1
75-99 2

thanks to all for looking.
• 12-10-2006
vart
int counters[4];

you will increase counter[0] for range 0-24, counter[1] for 25-49 etc
• 12-10-2006
bkamagnum
array
Quote:

Originally Posted by vart
int counters[4];

you will increase counter[0] for range 0-24, counter[1] for 25-49 etc

yea thats the easy way but i have to put it in an array help please!
• 12-10-2006
vart
int counter[4] IS an array of 4 elements
• 12-11-2006
bkamagnum
o i see i will use an array in my calculation function instead of my print function... thanks
• 12-11-2006
bkamagnum
help with counter
my counter isnt work right anyone know why???? thanks.

Code:

```#include<iostream> #include<iomanip> #include<stdio.h> #include<fstream> using namespace std; const int ARRAY_LENGTH = 8;     int readData(int range[8]); void printResults(int range[8]); int main() {       //Declaration Section       int range8[ARRAY_LENGTH];       int thearray8[ARRAY_LENGTH];       readData(range8);       printResults(thearray8);                   system("PAUSE");       return 0; }//end of main int readData(int range[8]) {       int grade;       int grades[17] = {35, 23, 23, 64, 75, 35, 23, 23, 64, 75, 35, 23, 23, 64, 75, 35, 23};       ifstream infile;       infile.open("C:/Documents and Settings/SmOkEbLaCk/Desktop/projects/proj8.txt");       infile>>grade;       while (infile)       {       if ((grade>=0) && (grade<=24))       range[1]++;             else if ((grade>=25) && (grade<=49))       range[2]++;             else if ((grade>=50) && (grade<=74))       range[3]++;             else if ((grade>=75) && (grade<=99))       range[4]++;             else if ((grade>=100) && (grade<=124))       range[5]++;             else if ((grade>=125) && (grade<=149))       range[6]++;             else if ((grade>=150) && (grade<=174))       range[7]++;             else       range[8]++;       infile>>grade;        }           infile.close();       return range[8]; }//end of function reading data void printResults(int range[8]) {     cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[1]<<endl;     cout<<"      25 - 49            "<<range[2]<<endl;     cout<<"      50 - 74            "<<range[3]<<endl;     cout<<"      75 - 99            "<<range[4]<<endl;     cout<<"      100 - 124          "<<range[5]<<endl;     cout<<"      125 - 149          "<<range[6]<<endl;     cout<<"      150 - 174          "<<range[7]<<endl;     cout<<"      175 - 200          "<<range[8]<<endl<<endl;     }//end of function printing results```
• 12-11-2006
vart
arrays are 0 based
index goes from 0 to 7 not 1-8

PS. Test for what you get with i = grade/25

also you fill one array and print values from another
• 12-11-2006
bkamagnum
counters
now the problem is that I get outrageous numbers...... help please. and thanks

Code:

```#include<iostream> #include<iomanip> #include<stdio.h> #include<fstream> using namespace std; const int ARRAY_LENGTH = 8;     int readData(int range[8]); void printResults(const int thearray[8]); int main() {       //Declaration Section       int range8[ARRAY_LENGTH];       int thearray8[ARRAY_LENGTH];       readData(range8);       printResults(thearray8);                   system("PAUSE");       return 0; }//end of main int readData(int range[8]) {       int grade=0;       ifstream infile;       infile.open("C:/Documents and Settings/SmOkEbLaCk/Desktop/projects/proj8.txt");       infile>>grade;           while (infile)       {       if ((grade>=0) && (grade<=24))       range[0]++;             else if ((grade>=25) && (grade<=49))       range[1]++;             else if ((grade>=50) && (grade<=74))       range[2]++;             else if ((grade>=75) && (grade<=99))       range[3]++;             else if ((grade>=100) && (grade<=124))       range[4]++;             else if ((grade>=125) && (grade<=149))       range[5]++;             else if ((grade>=150) && (grade<=174))       range[6]++;             else       range[7]++;       infile>>grade;        }       infile.close();       return range[8]; }//end of function reading data void printResults(const int thearray[8]) {     int range[8];               cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl;     }//end of function printing results```
• 12-11-2006
anon
Code:

```void printResults(const int thearray[8]) {     int range[8];               cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl;     }//end of function printing results```
Your just printing an uninitialized array here. (The fact that you use the same name, doesn't mean range points to the same data in both functions. Here you declare a new array.)

Code:

```      return range[8]; }//end of function reading data```
Here you return an array element which is out of bounds. Luckily you don't try to use it.

The general strategy would be to define an array in main. Pass it to the first function to read data. This function returns nothing, because it modifies the passed array.
Then pass the same array to the second function to print it.
• 12-11-2006
bkamagnum
array
Quote:

Originally Posted by anon
Code:

```void printResults(const int thearray[8]) {     int range[8];               cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl;     }//end of function printing results```
Your just printing an uninitialized array here. (The fact that you use the same name, doesn't mean range points to the same data in both functions. Here you declare a new array.)

Code:

```      return range[8]; }//end of function reading data```
Here you return an array element which is out of bounds. Luckily you don't try to use it.

The general strategy would be to define an array in main. Pass it to the first function to read data. This function returns nothing, because it modifies the passed array.
Then pass the same array to the second function to print it.

Code:

```void printResults(const int thearray[8]) {     int range8[ARRAY_LENGTH];     int thearray8[ARRAY_LENGTH];     readData(range8);     printResults(thearray8);       cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl;     }//end of function printing results```
by removing the intializing and readin in the fuction readData like so^, i get an error that says:
Code:

```84 C:\Documents and Settings\SmOkEbLaCk\Desktop\projects\project #8.4.cpp `range' undeclared (first use this function)   (Each undeclared identifier is reported only once for each function it appears in.)```
• 12-11-2006
vart
change to
void printResults(const int range[8])
• 12-11-2006
bkamagnum
cout
Quote:

Originally Posted by vart
change to
void printResults(const int range[8])

im sorry guys im a newbie to this stuff as you can tell ......

it work it is just that my counter is not work right.... i cant seem to find my error.
my output is:
http://tinypic.com/view/?pic=40b2sg6

Code:

```int readData(int range[8]) {       int grade=0;       ifstream infile;       infile.open("C:/Documents and Settings/SmOkEbLaCk/Desktop/projects/proj8.txt");       infile>>grade;           while (infile)       {       if ((grade>=0) && (grade<=24))       range[0]++;             else if ((grade>=25) && (grade<=49))       range[1]++;             else if ((grade>=50) && (grade<=74))       range[2]++;             else if ((grade>=75) && (grade<=99))       range[3]++;             else if ((grade>=100) && (grade<=124))       range[4]++;             else if ((grade>=125) && (grade<=149))       range[5]++;             else if ((grade>=150) && (grade<=174))       range[6]++;             else       range[7]++;       infile>>grade;        }       infile.close(); }//end of function reading data void printResults(const int range[8]) {     int range8[ARRAY_LENGTH];         readData(range8);         cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl;     }//end of function printing results```
• 12-11-2006
vart
Code:

```void printResults(const int range[8]) { //    int range8[ARRAY_LENGTH];        readData(range);         cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl;     }//end of function printing results```
• 12-11-2006
bkamagnum
Code:

```#include<iostream> #include<iomanip> #include<stdio.h> #include<fstream> using namespace std; const int ARRAY_LENGTH = 8;     int readData(int range[8]); void printResults(int thearray[8]); int main() {       //Declaration Section       int range[ARRAY_LENGTH];       int thearray8[ARRAY_LENGTH];       readData(range);       printResults(thearray8);                   system("PAUSE");       return 0; }//end of main int readData(int range[8]) {       int grade=0;       ifstream infile;       infile.open("H:\projects\proj8.txt");       infile>>grade;           while (infile)       {             for (grade = 0;grade < 8; grade++)             if ((grade>=0) && (grade<=24))       range[0]++;             else if ((grade>=25) && (grade<=49))       range[1]++;             else if ((grade>=50) && (grade<=74))       range[2]++;             else if ((grade>=75) && (grade<=99))       range[3]++;             else if ((grade>=100) && (grade<=124))       range[4]++;             else if ((grade>=125) && (grade<=149))       range[5]++;             else if ((grade>=150) && (grade<=174))       range[6]++;             else       range[7]++;       infile>>grade;        }       infile.close(); }//end of function reading data void printResults(int range[8]) {     readData(range);         cout<<"      RANGE:        NUMBER OF STUDENTS:"<<endl;     cout<<"      0 - 24              "<<range[0]<<endl;     cout<<"      25 - 49            "<<range[1]<<endl;     cout<<"      50 - 74            "<<range[2]<<endl;     cout<<"      75 - 99            "<<range[3]<<endl;     cout<<"      100 - 124          "<<range[4]<<endl;     cout<<"      125 - 149          "<<range[5]<<endl;     cout<<"      150 - 174          "<<range[6]<<endl;     cout<<"      175 - 200          "<<range[7]<<endl<<endl; }//end of function printing results```
alright i still have the counter problem
http://tinypic.com/view/?pic=40b2sg6
• 12-11-2006
Initialize each element of range[] to zero before you call the first ++ operator on the value.

Also, in looking quickly at your code I don't see the need for the for statement listed below:
Code:

```while (infile) {     for (grade = 0;grade < 8; grade++)```
and this:
Code:

```infile>>grade; while (infile) {     // whatever     infile >> grade; }```
could just as easily be this:
Code:

```while(infile >> grade) {     //whatever }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last