PDA

View Full Version : C++ Problem


Zekonja
10-18-2005, 02:17 AM
I need a code that can show which one of random typed positive numbers is the highest.. The highest number will be printed when a user types number 0. Thus, number 0 is used as a stop value in the prigram.....

help :)

nikkiH
10-18-2005, 03:08 AM
Homework?
This is way too easy to not be homework.
How about you post what you have so far and tell us where you are having issues instead.

Zekonja
10-18-2005, 03:53 AM
I'm newbe as you can see :o ...and yes this is my homework :-)

In previous exercise I should sort 3 numbers... I used bobble sort... (I am sure I could do it in a much easier way but don't know how) :D


#include <iostream>
using namespace std;

// Declaration of swap function
void swap(int &m, int &n);

int main()
{
int N = 3;

int b;
int c;
int d;

cout<<"Type three numbers"<<endl;
cin>>b>>c>>d;
int a[3] = {b,c,d};

// Selection Sort
for (int i = 0; i < (N - 1); i++)
{
int minIndex = i;

// Find the index of the minimum element
for (int j = i + 1; j < N; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}

// Swap if i-th element not already smallest
if (minIndex > i)
{
swap(a[i], a[minIndex]);
}
}


// Print sorted results
cout << The numbers are sorted from lowest to heighest"<<endl;
for (int i = 0; i < N; i++)
{
cout << i << " " << a[i] << endl;
}

return 0;
}

void swap(int &x, int &y)
{
int temp;

temp = x;
x = y;
y = temp;
}



And now I wrote the code for printing the input values:



#include <iostream>
using namespace std;

int main()
{
int a[5];
int n =0;

while (cin>>a[n]) {
n++;
}

cout<<"Yu've typed : \n";
for (int i=0; i<n; i++) {
cout<<a[i]<<"\n";
}

return 0;
}



Now I am trying to reuse this code in some way :D

oracleguy
10-18-2005, 04:29 AM
You can re-use your sorting method, just change the value of N to the new number of elements.

Zekonja
10-18-2005, 05:23 AM
I did it, almost :D

I just need a function or something which returns me the size of array a.... can you help me with that?

In the code below I jus typed integer 3 instead of the size of my array a :D its bolded.. i.e. the program will execute corectly only if you type 3 integers:

#include <iostream>

using namespace std;

void swap(int &m, int &n);

int main()
{
int a[100];
int n =0;


do {
cin>>a[n];
n++;
}

while(a[n-1]!=0);

for (int i = 0; i < ( 3 - 1); i++)
{
int minIndex = i;

// Find the index of the minimum element
for (int j = i + 1; j < 3; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}

// Swap if i-th element not already smallest
if (minIndex > i)
{
swap(a[i], a[minIndex]);
}
}

if (a[3-1]>0)

cout << "The highest number is "<< a[3-1] <<endl;

else
cout<< "You haven't typed any positive number.";

return 0;
}

void swap(int &x, int &y)
{
int temp;

temp = x;
x = y;
y = temp;
}

sage45
10-18-2005, 07:42 PM
You already have a way to tell the size of your array.

#include <iostream>

using namespace std;

void swap(int &m, int &n);

int main()
{
int a[100];
int n =0;


do {
cin>>a[n];
n++;
}

while((a[n-1]!=0) || (n != 100));

for (int i = 0; i < n; i++)
{
int minIndex = i;

// Find the index of the minimum element
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}

// Swap if i-th element not already smallest
if (minIndex > i)
{
swap(a[i], a[minIndex]);
}
}

if (a[n-1]>0)

cout << "The highest number is "<< a[n-1] <<endl;

else
cout<< "You haven't typed any positive number.";

return 0;
}

void swap(int &x, int &y)
{
int temp;

temp = x;
x = y;
y = temp;
}

HTH,

-sage-

DoubtlessOne
10-24-2005, 05:24 AM
#include <iostream>
#include <stdlib.h> //heh heh fun randomness!! ADD IT!! :D
using namespace std;

void swap(int &m, int &n);

int main()
{
int a[100];
int n =0;


do {
cin>>a[n]; //you are saying function a[n], which is still equal to 0 because a[100] function to n would be 100*0
n++;
}

while(a[n-1]!=0);

for (int i = 0; i < ( 3 - 1); i++)
{
int minIndex = i;

// Find the index of the minimum element
for (int j = i + 1; j < 3; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}

// Swap if i-th element not already smallest
if (minIndex > i)
{
swap(a[i], a[minIndex]);
}
}

if (a[3-1]>0)

cout << "The highest number is "<< a[3-1] <<endl;

else
cout<< "You haven't typed any positive number.";

return 0;
}

void swap(int &x, int &y)
{
int temp;

temp = x;
x = y;
y = temp;
}


a bit new to this... so my advice is basically useless :confused:

sage45
10-24-2005, 05:44 AM
int a[100];
int n =0;

do {
cin>>a[n]; //you are saying function a[n], which is still equal to 0 because a[100] function to n would be 100*0
n++;
}while(a[n-1]!=0);


As you can see, a is an array of 100 integers and n is declared as an integer which is set to 0.

When cin >> a[n] is used, he is saying take the console input stream and attempt to insert the data into a at location [n]. Since [n] is set to zero then the data will be placed into a[0]. After that n is incremented by one and the data in a[n - 1] is checked to see if it equals zero. Since n is now 1, then a[0] is checked.

Hope that clears this up for you.

-sage-