I've done some progress... I feel im so near now!!! Still got some problems left thought...
Heres my code
Code
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// sparar allt i en array
int elever(int seq[], int howMany)
{
const int STOP = 0;
int count = 0;
int h;
cout << "Enter height of student: "<<endl;
cin>>h;
while(h > STOP)
{
seq[count] = h;
count++;
if (count == howMany)
{
cout<<"fullt"<<endl;
break;
}
cin >> h;
}
return 0;
}
// Function med boublesort som sorterar efter min
double maxsort(int seq[], int howMany)
{
const int SIZE = 100;
int n;
cout << "Enter n: ";
cin >>n;
cout << endl;
for(int y = 1; y <= howMany + 1; y++)
for(int i = 0; i < howMany + 1; i++ )
{
if ( seq[ i ] > seq[ i + 1 ] )
{
int temp = seq[ i ];
seq[ i ] = seq[ i + 1 ];
seq[ i + 1 ] = temp;
}
}
for(int i = 1; i <= howMany; i++)
cout << seq[i-1] << " ";
}
// Function med boublesort som sorterar efter min
double minsort(int seq[], int howMany)
{
int n;
cout<<"enter n?"<<endl;
cin >>n;
for (int y = 1; y <= howMany + 1; y++ )
{
for (int i = 0; i < howMany + 1; i++ )
{
if ( seq[ i ] < seq[ i+1 ] )
{
int temp = seq[ i ];
seq[ i ] = seq[i + 1 ];
seq[ i + 1 ] = temp;
}
}
}
for(int i = n; i > 0; i--)
{
cout << seq[i+1]<<" ";
}
}
// en function som kollar att "n" är validated för programmet dvs 0<n<=class_size
int checkN(int seq[], int howMany)
{
int n;
const int SIZE = 100;
do{
cout << "Enter n ";
cin >> n;
}while(n > SIZE);
return 0;
}
void display_menu()
{
cout << setw(10) << setfill('*') << "*" << endl;
cout << "1. Display shortest" << endl;
cout << "2. Display tallest" << endl;
cout << setw(10) << setfill('*') << "*" << endl << endl;
}
int main()
{
int seq;
const int SIZE=100;
int seq1[SIZE];
int option;
int antal = 0;
int howMany =0;
antal = elever(seq1, SIZE);
//Display the menu
display_menu();
cout << "Your option? ";
cin >> option;
switch (option){
//1, Option1 display the height of the n tallest student
//sorted increasingly
case 1:
checkN(seq1, howMany);
maxsort(seq1, antal);
break;
//2, Option2 display the height of the n shortest student
//sorted increasingly
case 2:
checkN(seq1, howMany);
minsort(seq1, antal);
break;
}
return 0;
}
My code right now doesn't display the result correctly.. it just shows blank.. but if the user types a number larger than the heights wroten the program asks for n again.. but it should be like this..
"Enter the height of each student:
175 167 160 164 183 187 188 179 176 175
169 175 176 178 165 160 173 175 187 178 -1***************
1. Display shortest
2. Display tallest
***************
Option?
2Enter n:
30Enter n:
20160 160 164 165 165 167 169 173 175 175 175 176 176 178 178 179 183 187 187 188
"
This is your opportunity to earn easy FG!!