Alright, I've started learning array recenetly and have to write a program using them. I am having problems getting started on it, the notes we received seem very unclear. Here is the link to the instructions I have to use.
http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm7.htm

Basically I just want some pointers to get me going in the right direction. Here is what code I have so far, I'm already getting an error in main because it says buildAr is not declared.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

#define ARSIZE 20

int buildAr (int [], double [], double []);

int main()
    {
    int zid[ARSIZE];
    float pgmavg[ARSIZE];
    float testavg[ARSIZE];
    
    buildAR[zid];
    buildAR[pgmavg];
    buildAR[testavg];
    
    system ("pause");
    return 0;
    }
/*********************************************************/
int buildAr (int [], double [], double [])
    {
    int num;
    ifstream inFile;
    
    inFile.open( "averages.txt" );

    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    int zid[0];

    inFile >> num;
    
    //while ( inFile )
    
    }

Recommended Answers

All 24 Replies

lines 17 thru 19 looks like a data object because of the brackets [ and ]. Replace them with parentheses ( and ) to make it a function call.

Also note that buildAr() takes 3 parameters, not one. So you can consolidate lines 17-19 into just one line: buildAr(zip, pgmavg, testavg);

I still get an error in the compiler saying undeclared in int main().

I tried to compile your program:
1) check function name spelling and capitalization. They must match exactly.

2) Check data types of the arrays. You declared them as float but the function buildAr() expects doubles.

Ok cool I don't get the error anymore.

Declare an input file stream (see "buildAr notes" below) and any other variables that
you may need

Open the input file and make sure that it opened correctly (see "buildAr notes" below)

Initialize a subscript to the beginning of the array

Get the first student id from input (see "buildAr notes" below)

While there are records in the file (see "buildAr notes" below)

Put the student id into the appropriate array

Get a program average from input
Put the program average into the appropriate array

Get a test average from input
Put the test average into the appropriate array

Increment the subscript to the next spot in the array

Get the next student id from input
Endwhile

Close the input file (see "buildAr notes" below)

Return the number of students in an array (think about the subscript)

I was confused on how to initialize the array to the beginning because I thought it already was? For putting the values into the appropriate arrays any though on how I should do that?

I've made some progress with this but not much. If anyone could help guide me in the right direction that be great.

http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm7.htm

Here is the code I have at this point.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int buildAr(int [], double [], double []);
void displayAr(int [], double [], double [], int);

#define ARSIZE 20

int main()
    {
    int zid[ARSIZE];
    double pgmavg[ARSIZE];
    double testavg[ARSIZE];
    
    buildAr (zid, pgmavg, testavg);
    
    cout << "The UNSORTED student information.";
    
    cout << zid[ARSIZE];
    
    system ("pause");
    return 0;
    }
/*********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
    int num;
    int i;
    ifstream inFile;
    
    inFile.open( "averages.txt" );

    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    

    inFile >> num;
    
    while ( inFile )
    {
    zid[i] = num;
    inFile >> num;
    pgmavg[i] = num;
    inFile >> num;
    testavg[i] = num;
    inFile >> num;
    i++;
    }
    inFile.close();
    return (num);
}
/*********************************************************/
void dispalyAr(int zid [], double pgmavg [], double testavg [], int i)
{
     cout << zid[i];
}

Don't use [list] [/list] tags. [code=cplusplus] ... [/code] will do the job of adding line numbers just nicely.

line 22: That is not the correct way to display the contents of an array. You have to do it in a loop, and you need to display the contents of all three arrays.

for(int i = 0; i < 20; i++)
{
    cout << zid[i] << " " << ...
}

The above will only partially satisfy the requirements of your assignment. use the setw() function to space out the columns in the report cout << setw(20) << zid[i] << setw(25) << ... The numbers I put here are only guesses, you will have to test and modify them to suit yourself and the requiremenets of your assignment.

Sorting the arrays: I don't see a requirement to use any specific sort algorithm, so IMO the bubble sort algorithm is the easiest to code. Google for it and you will find lots of examples.

Alright, i'll have to give that a try when I get back to my computer in a few hours. What about the display function, what do I need to do to get that going.

The code snippet I previously posted above is part of that display function.

Ok this is where I am at now, when I run it the screen pops up for a quarter second then is gone. So any idea what the trouble could be?

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int buildAr(int [], double [], double []);
void displayAr(int [], double [], double [], int);

#define ARSIZE 20

int main()
    {
    int size;
    int sub;
    int zid[ARSIZE];
    double pgmavg[ARSIZE];
    double testavg[ARSIZE];
    
    buildAr (zid, pgmavg, testavg);
    
    size = buildAr (zid, pgmavg, testavg);
    
    displayAr (zid, pgmavg, testavg, sub);  
    
    system ("pause");
    return 0;
    }
/*********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
    int num;
    double num2, num3;
    int sub = 0;
    ifstream inFile;
    
    inFile.open( "averages.txt" );

    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    inFile >> num;
    
    while ( inFile )
    {
    zid[sub] = num;
    inFile >> num;
    sub++;
    pgmavg[sub] = num2;
    inFile >> num2;
    sub++;
    testavg[sub] = num3;
    inFile >> num3;
    sub++;
    }
    inFile.close();
    return (sub);
}
/*********************************************************/
void displayAr(int zid [], double pgmavg [], double testavg [], int size )
{
     for(int sub = 0; sub < 20; sub++)
     {
    cout << zid[sub] << pgmavg[sub] << testavg[sub];
     }
}

Add a system("PAUSE"); in your display function after the loop.

I just tried that, no luck. Any other suggestions?

eh, a really crude way would to make a counter in your loop and system("pause"); after the last cout<< but frankly I haven't figured out why the compiler "skips lines" either yet.

Well I made some changes and I get some output but it comes out with numbers that I should not be having, based on the file I am reading in from (which is in the link posted above its averages.txt). Here is the code I'm at not.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int buildAr(int [], double [], double []);
void displayAr(int [], double [], double [], int);

#define ARSIZE 20

int main()
    {
    int size;
    int sub;
    int zid[ARSIZE];
    double pgmavg[ARSIZE];
    double testavg[ARSIZE];
    
    buildAr (zid, pgmavg, testavg);
    
    size = buildAr (zid, pgmavg, testavg);
    
    displayAr (zid, pgmavg, testavg, sub);  
    
    system ("pause");
    return 0;
    }
/*********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
    int num;
    double num2, num3;
    int sub = 0;
    ifstream inFile;
    
    inFile.open( "averages.txt" );

    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    inFile >> num;
    
    while ( inFile )
    {
    zid[sub] = num;
    inFile >> num;
    pgmavg[sub] = num2;
    inFile >> num2;
    testavg[sub] = num3;
    inFile >> num3;
    sub++;
    }
    inFile.close();
    return (sub);
}
/*********************************************************/
void displayAr(int zid [], double pgmavg [], double testavg [], int size )
{
     for(int sub = 0; sub < 20; sub++)
     {
     cout << zid[sub] << pgmavg[sub] << testavg[sub];
     }
}

Two things, you do not declare declare sub in the main function, and int size in your void displayAr seems to be superflous. Another thing is in your buildAr function, shouldn't you be using inFile>>num before saying zid[sub] = num ect?

in the displayAR() function you have to put spaces between each number. as shown in my previous post that discusses the setw() function.

Thanks for the help so far everything else I've figured out and get it up to running just one last step left, I just need to work on my last function which sorts the output by using a selection sort algorithm. Any suggestions on that?

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int buildAr(int [], double [], double []);
void displayAr(int [], double [], double [], int);

#define ARSIZE 20

int main()
    {
    int size;
    int sub = 0;
    int zid[ARSIZE];
    double pgmavg[ARSIZE];
    double testavg[ARSIZE];
    
    buildAr (zid, pgmavg, testavg);
    
    size = buildAr (zid, pgmavg, testavg);
    
    cout << "The UNSORTED student information" << endl;
    displayAr (zid, pgmavg, testavg, sub);  
    
    system ("pause");
    
    
    system ("pause");
    return 0;
    }
/*********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
    int num;
    double num2, num3;
    int sub = 0;
    ifstream inFile;
    
    inFile.open( "averages.txt" );

    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    
    while ( inFile )
    {
    inFile >> num;
    zid[sub] = num;
    inFile >> num2;
    pgmavg[sub] = num2;
    inFile >> num3;
    testavg[sub] = num3;
    sub++;
    }
    inFile.close();
    return (sub);
}
/*********************************************************/
void displayAr(int zid [], double pgmavg [], double testavg [], int size)
{
     int sub = 0;
     double overall;
     cout << "Student ID       Program Average      Test Average   Overall Average" << endl;
     
     overall = (pgmavg[sub] * .4) + (testavg[sub] * .6);
     
     for(sub = 0; sub < 19; sub++)
     {
     cout << zid[sub] << "              "<< setw(6) << pgmavg[sub] <<"                "<< setw(4) << testavg[sub] << setw(4) << endl;
     }
}
/*********************************************************/
void sortAr(int zid [], double pgmavg [], double testavg [], int size)
{

Sort in which way? Greatest to least, least to greatest? Either way, you can either use sort() included with <algorithm> or create your own recursive sorting function.

It needs to be in ascending order.

Well in that case, take your inputs and make a for loop for the ammount of elments you have. Check to see if the presceding element is greater than the next, if it is; switch the two. After you finish this loop, make another loop to check if any element is greater than the next, if it is set a flag to false and exit the loop. Lastly, make an if statement to check if the flag is false, if it is false, call the sort function again. Be sure to set the flag to true at the begning of the function and if done properly, the list will sort it self continually until in correct order, after which the function will have ended.

Well this is where I am at not. It prints out but the data isn't sorted which is obviously problem so here is the code I got at this point.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int buildAr(int [], double [], double []);
void displayAr(int [], double [], double [], int);
void sortAr (int [], double [], double [], int);

#define ARSIZE 20

int main()
    {
    int size;
    int sub = 0;
    int zid[ARSIZE];
    double pgmavg[ARSIZE];
    double testavg[ARSIZE];
    
    buildAr (zid, pgmavg, testavg);
    
    size = buildAr (zid, pgmavg, testavg);
    
    cout << "The UNSORTED student information" << endl;
    displayAr (zid, pgmavg, testavg, sub);  
    
    system ("pause");
    
    cout <<"The SORTED student information" << endl;
    displayAr (zid, pgmavg, testavg, sub);  
    
    
    system ("pause");
    return 0;
    }
/*********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
    int num;
    double num2, num3;
    int sub = 0;
    ifstream inFile;
    
    inFile.open( "averages.txt" );

    if ( inFile.fail() )
       {
       cout << "input file did not open";
       exit(0);
       }
    
    while ( inFile )
    {
    inFile >> num;
    zid[sub] = num;
    inFile >> num2;
    pgmavg[sub] = num2;
    inFile >> num3;
    testavg[sub] = num3;
    sub++;
    }
    inFile.close();
    return (sub);
}
/*********************************************************/
void displayAr(int zid [], double pgmavg [], double testavg [], int size)
{
     int sub = 0;
     double overall;
     cout << "Student ID       Program Average      Test Average   Overall Average" << endl;
     
     overall = (pgmavg[sub] * .4) + (testavg[sub] * .6);
     
     for(sub = 0; sub < 19; sub++)
     {
     cout << zid[sub] << "              "<< setw(6) << pgmavg[sub] <<"                "<< setw(4) << testavg[sub] << setw(4) << endl;
     }
     sortAr (zid, pgmavg, testavg, sub);
     
}
/*********************************************************/
void sortAr(int zid [], double pgmavg [], double testavg [], int size)
{
    int sub = 0;
    int min;
    int x;
    double temp;
    
    for (sub = 0; sub < 19; sub++) 
        {
        min = sub;
        for (x = sub + 1; x < size; x++)
            {
            if(zid[sub] < zid[min])
                 min = x;
            }
        temp = zid[sub];
        zid[sub] = zid[min];
        zid[min] = temp;
        }
}

You never used your sort function ^^;

oh and you need make it recursive because the current function will only make one run, which is insufficient, add another loop to check it and toggle a flag.

Could anyone help me write this function for Selection Sort, it has to go in ascending order, I am at a stalemate with it.

void sortAr(int zid [], double pgmavg [], double testavg [], int size)
{
    int sub = 0;
    int min;
    int x;
    double temp;
    
    for (sub = 0; sub < 18; sub++) 
        {
        min = sub;
        for (x = sub + 1; x < size; x++)
            {
            if(zid[sub] < zid[min])
                 min = x;
            }
        temp = zid[sub];
        zid[sub] = zid[min];
        zid[min] = temp;
        }
}

make another loop to check it and toggle a flag, if it does not pass the check, call your function again, it is only running once; make it recursive!

for (x = sub + 1; x < size; x++)            
            if(zid[sub] < zid[min])
            flag = false;

       if(!flag)
           sortAr();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.