I know I already posted this but I guess I cant do it the way it was shown. I want a 2-dim array to print out on the same line as string word. I cant seem to figure out how to get a 2-dim array to do this, does anyone know how to do this?

void Sort(string word[],float grades[][8], int number)
{
	int i,j=o, temp;
	string aName;
	float aName2[][];

	for (i=0; i<(names - 1); i++)
	{
		temp = i;
		aName = worde[i];
		aName2=grades[i][j];
			
		for(int k=i+1; k<names; k++)
		{
			if (word[k] < aName)
			{
				aName = word[k];
				aName2[i][j]=grades[k][8];
				temp = k;
			}
		}
				word[temp] = word[i];
				grades[temp][j]=grades[i][j];
				word[i] = aName;
				grades[i][j]=aName2[][];
				
	}

}

Recommended Answers

All 5 Replies

the sort algorithm is wrong. You have to swap all elements of grades array at the same time that the string in word is swapped. You can delete aName2 array.

if (word[k] < aName)
{
    // first swap the string in the word array
    aName = word[k];
    word[k] = word[i];
    word[i] = aName;
    // now swap the elements of the float array
    for(int j = 0; j < 8; j++)
    {
         float temp = grades[k][j];
         grades[k][j] = grades[i][j];
         grades[i][j] = temp;
    }
}

I put into my code like this but Im getting errors in my numbers now. I think its because of were Im trying to sort and I cant figure out where I have too. Maybe sort in two different places? Please help.

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

using namespace std;

const int rows = 12;
const int cols = 8;

void HEADING();
void Sort (string [],string [],int [],float [][8]);
void AVERAGEDISPLAY(float average, float average2);
void displayarray(string [],string [],int [],float[][8], int);




int main()
{	
	ifstream inData;
	int names=0;
	string lastname[rows], firstname[rows];
	int studentID[40];
	int i=0,j;						//for rows and cols
	float sum=0,total, total2=0;    //subject and score totals
	float studentgrades[rows][cols];
	float average=0,average2=0;		//average subject and score averages
	

	inData.open("TextFile1.txt");
	
	HEADING();//display heading

	while (inData>>lastname[names]>>firstname[names]>>studentID[names])
	{
		cout<<lastname[names]<<" "<<firstname[names]<<" "<<studentID[names]<<" ";
		
            
			for (j=0; j<cols; j++)//loop for studentgrades
            { 
	         inData >> studentgrades[i][j];
	         cout<<fixed<<setprecision(1)<<setw(5)<<studentgrades[i][j]<<" ";
			 sum=sum+studentgrades[i][j];//adds subject grade for individual student
			
            }
			total2=sum/cols;//averages individual student grade
			cout<<setw(5)<<total2;
			average+=total2;//adds for average grade
			sum=0;
			i++;
			cout<<endl;		
			Sort(lastname,firstname,studentID,studentgrades);	
	}
		
		cout<<endl;
		cout<<"SUBJECT AVERAGES ARE:";
		
		for (j=0; j<cols; j++)//loop for subject averages
		{	total=0;
            for (i=0; i<rows; i++)
            { 
	         inData >> studentgrades[i][j];
			 total+=studentgrades[i][j];//adds subjects only
            }
			cout<<fixed<<setprecision(1)<<setw(5)<<total/rows<<" ";
			average2+=total/rows;//adds subject averages
		}


	AVERAGEDISPLAY(average, average2);//displays student and subject grade averages
	displayarray(lastname, firstname,studentID,studentgrades, rows);
	inData.close();

return 0;
}
//****************************************************************************************
//****************************************************************************************
void HEADING ()
{
cout<<"-Student Name   ID    SUB1  SUB2  SUB3  SUB4  SUB5  SUB6  SUB7  SUB8 AVERAGE-"<<endl;
	cout<<"----------------------------------------------------------------------------";
	cout<<endl<<endl;
}
void Sort(string lastname[],string firstname [],int studentID [],float studentgrades[][8])
{
	int i, temp;
	string aName;
	string aName2;
	int aName3;

	for (i=0; i<(rows - 1); i++)
	{
		temp = i;
		aName = lastname[i];
		aName2 = firstname[i];
		aName3=studentID[i];
			
		for(int k=i+1; k<rows; k++)
		{
			if (lastname[k] < aName)
			{
				aName = lastname[k];
				lastname[k] = lastname[i];
				lastname[i] = aName;

				aName2=firstname[k];
				firstname[k]=firstname[i];
				firstname[i]=aName2;

				aName3=studentID[k];
				studentID[k]=studentID[i];
				studentID[i]=aName3;
				for(int j = 0; j <= 8; j++)
				{
					float temp = studentgrades[k][j];
					studentgrades[k][j] = studentgrades[i][j];
					studentgrades[i][j] = temp;
				}
			}
		}
				
				
	}

}
void AVERAGEDISPLAY(float average, float average2)
{
	cout<<endl<<endl;
	cout<<"AVERAGE SCORE USING STUDENT AVERAGES:"<<average/rows<<endl;//averages student averages
	cout<<"AVERAGE SCORE USING SUBJECT AVERAGES:"<<average2/cols<<endl<<endl;//averages subject averages
}
void displayarray(string lastname [],string firstname[],int studentID [],float studentgrades [][8], int names)
{
	for(int count=0;count<names;count++)
	{
		cout<<lastname[count]<<" "<<firstname[count]<<" "<<studentID[count]<<" ";
		for (int j=0; j<cols; j++)//loop for studentgrades
		{
cout<<studentgrades[count][j]<<" ";
		}
		cout<<endl;
	}

}

your program is close, but close isn't good enough. The call to Sort() is in the wrong place -- only call it once after all the data file has been read. Also the use of some variables is inconsistent in the loop that reads the data file. I also made a slight change to the sort algorithm.

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

using namespace std;

const int rows = 12;
const int cols = 8;

void HEADING();
void Sort (string [],string [],int [],float [][8]);
void AVERAGEDISPLAY(float average, float average2);
void displayarray(string [],string [],int [],float[][8], int);




int main()
{	
	ifstream inData;
	int names=0;
	string lastname[rows], firstname[rows];
    int studentID[rows] = {0};
	int i=0,j;						//for rows and cols
	float sum=0,total, total2=0;    //subject and score totals
    float studentgrades[rows][cols] = {0};
	float average=0,average2=0;		//average subject and score averages
	

	inData.open("..\\TextFile1.txt");
	
	HEADING();//display heading

	while (inData>>lastname[names]>>firstname[names]>>studentID[names])
	{
		cout<<lastname[names]<<" "<<firstname[names]<<" "<<studentID[names]<<" ";
		
            
			for (j=0; j<cols; j++)//loop for studentgrades
            { 
	         inData >> studentgrades[names][j];
	         cout<<fixed<<setprecision(1)<<setw(5)<<studentgrades[names][j]<<" ";
			 sum=sum+studentgrades[names][j];//adds subject grade for individual student
			
            }
			total2=sum/cols;//averages individual student grade
			cout<<setw(5)<<total2;
			average+=total2;//adds for average grade
			sum=0;
			names++;
			cout<<endl;		
	}
    Sort(lastname,firstname,studentID,studentgrades);	
		cout<<endl;
		cout<<"SUBJECT AVERAGES ARE:";
		
		for (j=0; j<cols; j++)//loop for subject averages
		{	total=0;
            for (i=0; i<rows; i++)
            { 
	         inData >> studentgrades[i][j];
			 total+=studentgrades[i][j];//adds subjects only
            }
			cout<<fixed<<setprecision(1)<<setw(5)<<total/rows<<" ";
			average2+=total/rows;//adds subject averages
		}


	AVERAGEDISPLAY(average, average2);//displays student and subject grade averages
	displayarray(lastname, firstname,studentID,studentgrades, rows);
	inData.close();

return 0;
}
//****************************************************************************************
//****************************************************************************************
void HEADING ()
{
cout<<"-Student Name   ID    SUB1  SUB2  SUB3  SUB4  SUB5  SUB6  SUB7  SUB8 AVERAGE-"<<endl;
	cout<<"----------------------------------------------------------------------------";
	cout<<endl<<endl;
}
void Sort(string lastname[],string firstname [],int studentID [],float studentgrades[][cols])
{
	int i, aName3;
	string aName;
	string aName2;

	for (i=0; i<(rows - 1); i++)
	{			
		for(int k=i+1; k<rows; k++)
		{
			if (lastname[k] < lastname[i])
			{
				aName = lastname[k];
				lastname[k] = lastname[i];
				lastname[i] = aName;

				aName2=firstname[k];
				firstname[k]=firstname[i];
				firstname[i]=aName2;

				aName3=studentID[k];
				studentID[k]=studentID[i];
				studentID[i]=aName3;
				for(int j = 0; j < cols; j++)
				{
					float temp = studentgrades[k][j];
					studentgrades[k][j] = studentgrades[i][j];
					studentgrades[i][j] = temp;
				}
			}
		}
				
				
	}

}
void AVERAGEDISPLAY(float average, float average2)
{
	cout<<endl<<endl;
	cout<<"AVERAGE SCORE USING STUDENT AVERAGES:"<<average/rows<<endl;//averages student averages
	cout<<"AVERAGE SCORE USING SUBJECT AVERAGES:"<<average2/cols<<endl<<endl;//averages subject averages
}
void displayarray(string lastname [],string firstname[],int studentID [],float studentgrades [][8], int names)
{
	for(int count=0;count<names;count++)
	{
		cout<<lastname[count]<<" "<<firstname[count]<<" "<<studentID[count]<<" ";
		for (int j=0; j<cols; j++)//loop for studentgrades
		{
cout<<studentgrades[count][j]<<" ";
		}
		cout<<endl;
	}

}

Dude, complete life saver! I really appreciate the help.

If you have learned about structures and classes yet it would be a lot easier on you if you created a structure or class to contain the data for one student, then read the data into an array of structures. The sort program then only has to sort one thing instead of 4 of 5.

struct student
{
    string lastname;
    string firstname;
    int studentID;
    float grades[cols];
};

struct student studentList[rows];

// sort
Sort(studentList[])
{
    for(int i = 0; i < rows-1; i++)
    {
         for(int j = i+1; j < rows; j++)
         {
               if( studentList[i] < studentList[j] )
               {
                    struct student temp = studentList[i];
                    studentList[i] = studentList[j];
                    studentList[j] = temp;
               }
          }
      }
}
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.