This program works perfectly except for one flaw, the sentence "Enter at most 50..." is printed a lot in the beginning of the program when it runs....why??
#include<iostream>
using namespace std;
int GET_DATA (int[]);
int get_num();
void SORT (int, int[]);
void PRINT_SORTED_ARRAY (int, int[]);
void DISPLAY (int, int[]);
int main ()
{
int n, data[50];
n=GET_DATA(data);
SORT (n, data);
PRINT_SORTED_ARRAY (n, data);
DISPLAY (n, data);
return 0;
}
int GET_DATA (int a[])
{
int n=0, num;
num=get_num();
while (num>0 && n<50)
{
a[n]=num;
n++;
num=get_num();
}
return n;
}
int get_num()
{
int n;
cout << "Enter at most 50 positive integers in the range 1-9, enter 0 when you are done.\n"; //THIS sentence is repeated a few times in the code....I have no clue why!!
cin >> n;
if (n<0 || n>9)
{
cout << "ERROR";
}
return n;
}
void SORT (int n, int a[])
{
int x, y, z;
for (x=0; x<n-1; x++)
for (y=x; y<n; y++)
if (a[x]>a[y])
{
z=a[x];
a[x]=a[y];
a[y]=z;
}
}
void PRINT_SORTED_ARRAY (int n, int a[])
{
int i;
cout <<"Sorted array:\n";
for (i=0; i<n; i++)
{
cout <<a[i]<< " ";
if ((i+1)%10==0)
{
cout<<endl;
}
}
}
void DISPLAY (int n, int a[])
{
int count, i, j;
cout <<"\nOUTPUT:\n";
cout <<"N\tCount\n";
for (i=1; i<=9; i++)
{
count=0;
for (j=0; j<n; j++)
if (a[j]==i)
{
count++;
}
if (count>0)
{
cout<<i<<"\t"<<count<<endl;
}
}
}
4 Replies - 239 Views - Last Post: 26 April 2011 - 08:01 PM
#1
Why is my c++ program repeating a certain sentence in the function?
Posted 26 April 2011 - 07:37 PM
Replies To: Why is my c++ program repeating a certain sentence in the function?
#2
Re: Why is my c++ program repeating a certain sentence in the function?
Posted 26 April 2011 - 07:50 PM
Sorry - deleted.
This post has been edited by r.stiltskin: 26 April 2011 - 07:58 PM
#3
Re: Why is my c++ program repeating a certain sentence in the function?
Posted 26 April 2011 - 07:57 PM
I had to change "num=get_num():" to "cin>>num;" thanx for helping
This post has been edited by adoleh: 26 April 2011 - 08:05 PM
#4
Re: Why is my c++ program repeating a certain sentence in the function?
Posted 26 April 2011 - 07:59 PM
OK, sorry, it's similar but not the same.
So, what is the first thing that your main function does (after declaring its variables)?
And what is the first thing GET_DATA does (after declaring its variables)?
And what is the first thing that get_num does (after declaring its variable)?
So, what is the first thing that your main function does (after declaring its variables)?
And what is the first thing GET_DATA does (after declaring its variables)?
And what is the first thing that get_num does (after declaring its variable)?
#5
Re: Why is my c++ program repeating a certain sentence in the function?
Posted 26 April 2011 - 08:01 PM
I fixed it don't worry. Geez, came here for help and got accused. Anyways, works now. Thanx
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|