numbers in reverse

program to read 10 numbers integers in reverse order

Page 1 of 1

4 Replies - 2941 Views - Last Post: 20 December 2009 - 05:09 AM Rate Topic: -----

#1 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

numbers in reverse

Post icon  Posted 19 December 2009 - 05:25 AM

I need to write a program to read 10 numbers (integers) and write them back in reverse orders. I have the first part of it started, but I'm not very good at programming and having a hard time understanding what to do next. I have a book but it don't help me to understand. I need help to walk me through some things. I did compile this and it runs but has a warning, warning C4508: 'main' : function should return a value; 'void' return type assumed.

//Lab8B.cpp
//Created by Charles Braun

#include <iostream>   
using namespace std;

int main()
{
   int x[10];
   int i;
   cout << "Type in your 10 numbers" << endl;
   for (i=0; i<10; i++)
	  cin >> x[i];

} 



Is This A Good Question/Topic? 0
  • +

Replies To: numbers in reverse

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: numbers in reverse

Posted 19 December 2009 - 05:50 AM

Your compiler is wrong to warn about lack of return 0; (C++ adds it automatically). Its even more wrong to "assume" void main.

What compiler are you using? if it comes out with messages like that, I'd be slightly worried that it might have other things wrong with it too.


As for the logical flow of your program - when thinking out a solution consider how you'd do it on paper first; look at the structure of your for loop and consider how you could reverse it:
You retrieved your numbers by way of array indexes: starting at 0 in ascending order, using i++ to increment, up to 9.

This post has been edited by Bench: 19 December 2009 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

Re: numbers in reverse

Posted 19 December 2009 - 10:47 PM

Quote

so I should just reverse the for line? i compiled it and it runs but does not show the numbers in reverse.

#include <iostream>   
using namespace std;

int main()
{
   int x[10];
   int i;
   cout << "Type in your 10 numbers" << endl;
   for (i=0; i<10; i++)
	  cin >> x[i];
	cout << endl << endl;

	cout << "the numbers you typed in reverse are" << endl;

	for (i--; i>10; i=0)
		cin >> x[i];
	cout << endl << endl;

	return 0;

} 


Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6703
  • View blog
  • Posts: 31,084
  • Joined: 10-May 07

Re: numbers in reverse

Posted 19 December 2009 - 10:59 PM

Why are you using cin twice?

If you want to display the numbers (from the array) then you want to print onto standard out, not read in from it.
Was This Post Helpful? 1
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: numbers in reverse

Posted 20 December 2009 - 05:09 AM

No, don't reverse the for statement itself, reverse the logic


First understand what a for statement actually does. go right back to basics to a problem with huge amounts of redundant repeated code.
int array[4] = { 10, 20, 30, 40 };

cout << "number at " << 0 << " is " << array[0] << endl;
cout << "number at " << 1 << " is " << array[1] << endl;
cout << "number at " << 2 << " is " << array[2] << endl;
cout << "number at " << 3 << " is " << array[3] << endl; 

output:
number at 0 is 10
number at 1 is 20
number at 2 is 30
number at 3 is 40 



Each statement in the code above is identical apart from the numbers
cout << "number at " << 0 << " is " << array[0] << endl;
cout << "number at " << 1 << " is " << array[1] << endl;
cout << "number at " << 2 << " is " << array[2] << endl;
cout << "number at " << 3 << " is " << array[3] << endl;

So, to simplify that, replace the number with an unknown (a "variable")
int X;
X = 0;
cout << "number at " << X << " is " << array[X] << endl;
X = 1;
cout << "number at " << X << " is " << array[X] << endl;
X = 2;
cout << "number at " << X << " is " << array[X] << endl;
X = 3;
cout << "number at " << X << " is " << array[X] << endl;


The X numbers are related, all that's happening is that X is increasing by 1 each time - the ++ operator will do that, it does the same thing as writing X = X+1;

int X = 0;
cout << "number at " << X << " is " << array[X] << endl;
X++;
cout << "number at " << X << " is " << array[X] << endl;
X++;
cout << "number at " << X << " is " << array[X] << endl;
X++;
cout << "number at " << X << " is " << array[X] << endl;

Now every single one of these statements are completely identical! the pattern can be broken down into a few steps
(1) int X = 0; create a variable called X initialised to zero.
(2) cout << "number at " << X << " is " << array[X] << endl; display something using X
(3) X++; modify X
- and Repeat!

Look at the format of a for loop, and how that fits the pattern above
for ( initialiser; condition; modifier )
{
	do something
} 

step (1) is the initialiser
step (2) is the "do something"
step (3) is the modifier

The only thing missing is the condition. The code which started out with all the redundant copy + paste didn't have one, but it could do. You know it wants to repeat 4 times, so it could be modified somewhat
int X = 0;
if( X < 4 )
{
    cout << "number at " << X << " is " << array[X] << endl;
    X++;
}
if( X < 4 )
{
    cout << "number at " << X << " is " << array[X] << endl;
    X++;
}
if( X < 4 )
{
    cout << "number at " << X << " is " << array[X] << endl;
    X++;
}
if( X < 4 )
{
    cout << "number at " << X << " is " << array[X] << endl;
    X++;
}


That code could now be copy+pasted til the cows come home, and the condition which checks that X is less than 4 will prevent the number ever being displayed for values beyond 3 (i.e., 4 times from 0 to 3)

SO the final, missing, piece of the for statement, the condition is X < 4

which means this
for ( initialiser; condition; modifier )
{
	do something
} 

becomes this
for ( int X = 0; X < 4; X++ )
{
	cout << "number at " << X << " is " << array[X] << endl;
} 




There's the logic for counting upwards broken into micro-chunks. The format of a for loop is fixed, however the logic which drives the way a for loop counts depends on the individual variables and components of a for loop.

This post has been edited by Bench: 20 December 2009 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1