I am writing this code, using arrays to read a list of students name, gpa and student number and sorting them out and then allowing the user to search through it.
Example input file -
Carlo 4861 2.56
Heather 8567 3.43
Kyle 5545 3.99
Would appreciate if someone can go over my code and help me.. thanks in advance -
Question
punjboy
Hello,
I am writing this code, using arrays to read a list of students name, gpa and student number and sorting them out and then allowing the user to search through it.
Example input file -
Carlo 4861 2.56
Heather 8567 3.43
Kyle 5545 3.99
Would appreciate if someone can go over my code and help me.. thanks in advance -
/*****************************************************************************************************************
Student System
***********************************************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
#define SMAX 50
int read_array (string s,float x[],float y[],int n);
void sort_array(float x[],float y[],int n);
string search_array(
/**********************************************
Main Function
***********************************************/
void main()
{int i,n;
float x[sMAX],y[sMAX];
string s;
// Get User Input
cout << "Enter Student Name :" ;
cin >> s;
// Entering Value into Array
n = read_array(s,x,y,SMAX);
// Sort Function
sort_array(x,y,n);
// Output
for (i=0li<n;i++)
{cout << x,y;
};
}
/**************************************************************
read_array()
Reading data from input file into array
*******************************************************************************************************/
int read_array(string s,float x[], float y[],int n)
{int i;
fstream f;
// open file
f.open('c:\\civ3\student.txt',ios::in);
if (!f.is_open()) return 0;
i = 0;
while (!f.eof())
{ f >> x >> y;
if(f.good()) i++;
};
cout << i;
// close file
f.close();
return i;
}
/***********************************************************************
sorting function
***********************************************************************/
void sort_array(float x[], float y[], int n)
{int i,j;
float temp;
temp = 0;
for (i = 0 ; i < n ; i++)
for (j = 0; j < (n-1) ; j++)
if (x[j] > x [j+1])
{ temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
temp = y[j];
y[j] = y[j+1];
y[j+1] = temp;
};
}
/*******************************************************************
Searching Array
********************************************************************/
Still have to write for Searching...
Link to comment
Share on other sites
4 answers to this question
Recommended Posts