Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

would someone please correct my code

Status
Not open for further replies.

gtj

Technical User
May 4, 2002
2
US
Hi,

I have to write a small c++ program that will return the first term (number) of a sequence of positive integers that is less than the immediately preceding term of the sequence.

for example if I enter {6, 5, 7, 8, 9} it will return 5 because it is less than the immediately preceding number. If I enter another array of numbers like {1, 2, 3, 5, 4, 6} it will return 4. and if the array is already in order (that is there is no term less than the immediately preceding term ) it will return the location zero - example {1, 2, 3, 4}, then 0 is returned. I should not use const.

This is my code so far... somewhere something is not correct. please help. I will greatly appreciate it.

GTJ
greggtrent@hotmail.com

Code:
#include <iostream.h>
 
void main()
{
int location = 0;
int i = 2;
int n=15;
int *a = new int[n];
	  
cout << &quot;This program will return the first term/number of a sequence of positive integers&quot; << endl;
cout << &quot;that is less than the immediately preceding term of the sequence.&quot; << endl << endl ;
 
cout << &quot;Enter the sequence of integers: &quot;;
cin >> a[n];

a = new int[n]; 

while (i <= n && location == 0) 
{
if (a[i] < (a[i] - 1))
location = i;
else i = i + 1;
}

cout << endl << &quot;The location of the first term is &quot; << a[i] << endl << endl;
}


 
There you go.

#include <iostream.h>
void main()
{
int location = 0;
int i = 0;
int n;
cout << &quot;This program will return the first term/number of a sequence of positive integers&quot; << endl;
cout << &quot;that is less than the immediately preceding term of the sequence.&quot; << endl << endl ;
cout << &quot;How many terms that your sequence will have ?&quot;<<endl;
cin>>n;
int *a = new int[n];
cout << &quot;Enter the sequence of integers: &quot;<<endl;
for( int j = 0; j < n; j++ )
cin >> a[j];
while (i <= n && location == 0)
{
if ( a < a[i - 1] )
location = i;
else i = i + 1;
}
cout << endl << &quot;The location of the first term is &quot; << i << endl;
cout << &quot;The first term is &quot;<<a<< endl << endl;
delete a;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top