d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Problem Calling Functions..
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 6 2013 10:21am
Hey im back again :wallbash: , this time Im having trouble calling a function.. my code looks like this.. it complies atleast right now but I cant use my functions correctly.

I wished that I could use the input in "getline(cin, input);" in the funcitons "maxsort" or "minsort" depending which option the user chooses. And after that call the function "int checkN(double howMany, int seq[])" to choose how many classmembers that should be displayed.

Code
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


// Function med boublesort som sorterar efter max


double maxsort(double howMany, int seq[])
{
for ( int pass = 1; pass <= howMany - 1; pass++ ) // passes

for (int i = 0; i < howMany - 1; i++ ) // one pass
{

if ( seq[ i ] > seq[ i + 1 ] ) // compare a pair of adjacent values
{
//swap
int temp = seq[ i ];
seq[ i ] = seq[ i + 1 ];
seq[ i + 1 ] = temp;
}
}
}
// Function med boublesort som sorterar efter min
double minsort(double howMany, int seq[])
{
for ( int pass = 1; pass <= howMany - 1; pass++ ) // passes

for (int i = 0; i < howMany - 1; i++ ) // one pass
{

if ( seq[ i ] < seq[ i + 1 ] ) // compare a pair of adjacent values
{
//swap
int temp = seq[ i ];
seq[ i ] = seq[ i + 1 ];
seq[ i + 1 ] = temp;
}
}
}

// en function som kollar att "n" är validated för programmet dvs 0<n<=class_size
int checkN(double howMany, int seq[])
{
const int SIZE = 100;
cout << "How big is the sequence: ";
cin >> howMany;


if (howMany > SIZE || howMany < 0)
cout << "Wrong input, max size is 100 and must be largar than 0" << endl;
else
{
for(int i = 0; i < howMany; i++)
cout << seq[i] << endl;

}

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()
{

//declare any other needed variables and constants here
int option;
int seq;
string input;
int const SIZE=100;

cout << "Enter the height of each student: ";
cout << endl;
getline(cin, input);
cout << endl;


//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:
cout << "Enter n: ";
cin >> seq;
cout << checkN;
break;
//2, Option2 display the height of the n shortest student
//sorted increasingly
case 2:
cout << "Enter n: ";
cin >> seq;
cout << checkN;
break;
//Read user option
//Handle user option
}


return 0;

}



In the end it should look something like this (the negative number should cancel the "getline" and move on to next part)

Green is numbers the user types in

"
Enter the height of each student:
175 167 160 164 183 187 188 179 176 175
168 175 176 178 165 160 173 165 187 178 -1


*****************
1. Display shortest
2. Display tallest
*****************
Option? 1
Enter n: 4
160 160 164 165
"
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 6 2013 11:10am
if someone help me out and explains good I'll donante fg... ty in advance..
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 6 2013 04:18pm
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? 2
Enter n: 30
Enter n: 20
160 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!!
Member
Posts: 1,177
Joined: Feb 19 2006
Gold: 962.70
Oct 9 2013 01:18am
Don't know if you still need this... or if you are limited to only using certain concepts (I'm assuming this is homework) but here's how I would solve what I think you are trying to do

Code

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

void displayInstructions() {
cout << "Enter the height of each student, separated by a new line" << endl
<< "Enter '-1' to finish" << endl;
}

int getSortOrderChoice() {
cout << "Choose your sorting order" << endl
<< " 1. Shortest -> Tallest" << endl
<< " 2. Tallest -> Shortest" << endl;

int sortOrder = 0;
cin >> sortOrder;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

while (sortOrder != 1 && sortOrder != 2) {
cout << "Please input either 1 or 2" << endl;
cin >> sortOrder;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return sortOrder;
}

int getDataPointsChoice(int maxDataPoints) {
cout << "How many data points to display?" << endl;

int dataPoints = 0;
cin >> dataPoints;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

while (dataPoints <= 0 || dataPoints > maxDataPoints) {
cout << "Please enter an integer between 1 and " << maxDataPoints << endl;
cin >> dataPoints;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return dataPoints;
}

void enterData(vector<int> &data) {

int dataPoint = 0;

while (cin >> dataPoint && dataPoint != -1) {
data.push_back(dataPoint);
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}

void bubbleSort(vector<int> &data, int sortOrder) {

for(unsigned int i = 0; i < data.size(); i++) {
for(unsigned int j = i+1; j < data.size(); j++) {
if(sortOrder == 1) {
if(data[i] > data[j])
swap(data[i], data[j]);
} else {
if(data[i] < data[j])
swap(data[i], data[j]);
}
}
}
}

void printDataPoints(vector<int> &data, int dataPoints) {
data.resize(dataPoints);
for (vector<int>::const_iterator i = data.begin(); i != data.end(); ++i)
cout << *i << " ";
cout << endl;
}

int main(int argc, char* argv) {

vector<int> input;

displayInstructions();
enterData(input);

int sortOrder = getSortOrderChoice();
int dataPoints = getDataPointsChoice(input.size());

bubbleSort(input, sortOrder);

printDataPoints(input, dataPoints);
}


LMK if you have more questions
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll