1. ## 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.

2. int counters[4];

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

3. ## array

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!

4. int counter[4] IS an array of 4 elements

5. o i see i will use an array in my calculation function instead of my print function... thanks

6. ## 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;

void printResults(int range[8]);

int main()
{
//Declaration Section
int range8[ARRAY_LENGTH];
int thearray8[ARRAY_LENGTH];

printResults(thearray8);

system("PAUSE");
return 0;
}//end of main

{
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");

while (infile)
{
range[1]++;

range[2]++;

range[3]++;

range[4]++;

range[5]++;

range[6]++;

range[7]++;

else
range[8]++;

}

infile.close();
return range[8];

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```

7. 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

8. ## 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;

void printResults(const int thearray[8]);

int main()
{
//Declaration Section
int range8[ARRAY_LENGTH];
int thearray8[ARRAY_LENGTH];

printResults(thearray8);

system("PAUSE");
return 0;
}//end of main

{
ifstream infile;
infile.open("C:/Documents and Settings/SmOkEbLaCk/Desktop/projects/proj8.txt");

while (infile)
{
range[0]++;

range[1]++;

range[2]++;

range[3]++;

range[4]++;

range[5]++;

range[6]++;

else
range[7]++;

}
infile.close();
return range[8];

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```

9. 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];
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.

10. ## array

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];
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];

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.)```

11. change to
void printResults(const int range[8])

12. ## cout

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])
{
ifstream infile;
infile.open("C:/Documents and Settings/SmOkEbLaCk/Desktop/projects/proj8.txt");

while (infile)
{
range[0]++;

range[1]++;

range[2]++;

range[3]++;

range[4]++;

range[5]++;

range[6]++;

else
range[7]++;

}
infile.close();

void printResults(const int range[8])
{
int range8[ARRAY_LENGTH];

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```

13. Code:
```void printResults(const int range[8])
{
//     int range8[ARRAY_LENGTH];

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```

14. Code:
```#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<fstream>

using namespace std;

const int ARRAY_LENGTH = 8;

void printResults(int thearray[8]);

int main()
{
//Declaration Section
int range[ARRAY_LENGTH];
int thearray8[ARRAY_LENGTH];

printResults(thearray8);

system("PAUSE");
return 0;
}//end of main

{
ifstream infile;
infile.open("H:\projects\proj8.txt");

while (infile)
{

range[0]++;

range[1]++;

range[2]++;

range[3]++;

range[4]++;

range[5]++;

range[6]++;

else
range[7]++;

}
infile.close();

void printResults(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```
alright i still have the counter problem
http://tinypic.com/view/?pic=40b2sg6

15. 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)
{
and this:
Code:
```infile>>grade;
while (infile)
{
// whatever
```while(infile >> grade)