Thread: array help

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    Question 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. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int counters[4];

    you will increase counter[0] for range 0-24, counter[1] for 25-49 etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    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!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int counter[4] IS an array of 4 elements
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    o i see i will use an array in my calculation function instead of my print function... thanks

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    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

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    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

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    Last edited by anon; 12-11-2006 at 10:08 AM.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    9

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

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    change to
    void printResults(const int range[8])
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    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
    Last edited by bkamagnum; 12-11-2006 at 10:34 AM.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    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

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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
    }
    Last edited by elad; 12-11-2006 at 12:53 PM.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM