Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,889 people online right now. Registration is fast and FREE... Join Now!




print list (reverse numbers in array)

 
Reply to this topicStart new topic

print list (reverse numbers in array)

kkgaming
15 Mar, 2007 - 11:02 PM
Post #1

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
CODE

#include <stdio.h>

//Start of Print List Iteration Fuctions

void printList(int thelist[], int howmany)
{
  int i;

  for (i = 0; i < howmany; ++i)
  printf("%d: %d\n", i, thelist[i]);

}


int main()
{
  
//Print out iterative Print List
   int List[5];
   int pattern;
   int lsize=6;
   int i;
   int res;
   int x;
  
  
   for (i =0; i < lsize; i++)
   List[i] = i;
  
   printList(List,lsize);

  
  return 0;
  
  
}




I typed this up but want to know how I reverse the order of the numbers like
5
4
3
2
1

(the second batch of numbers I was wondering how to reverse)

also how would I do this program recrusively?

This post has been edited by kkgaming: 15 Mar, 2007 - 11:06 PM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Print List (reverse Numbers In Array)
15 Mar, 2007 - 11:44 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(kkgaming @ 16 Mar, 2007 - 12:32 PM) *

CODE

#include <stdio.h>

//Start of Print List Iteration Fuctions

void printList(int thelist[], int howmany)
{
  int i;

  for (i = 0; i < howmany; ++i)
  printf("%d: %d\n", i, thelist[i]);

}


int main()
{
  
//Print out iterative Print List
   int List[5];
   int pattern;
   int lsize=6;
   int i;
   int res;
   int x;
  
  
   for (i =0; i < lsize; i++)
   List[i] = i;
  
   printList(List,lsize);

  
  return 0;
  
  
}




I typed this up but want to know how I reverse the order of the numbers like
5
4
3
2
1

(the second batch of numbers I was wondering how to reverse)

also how would I do this program recrusively?


all you need to do is have a for loop like for(i=howmany;i>=0;i--)

and for recursion the logic could be

send an index number, length of array and array to function.
and do this let's say func name is "recursive"

CODE


if(index>=length) return;
recursive(array,index+1,length);
printf("%d",array[index]);



soooo, simple biggrin.gif

hope this will help you. smile.gif

User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Print List (reverse Numbers In Array)
16 Mar, 2007 - 12:04 AM
Post #3

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
that will reverse both sets though won't it? I just want the second part fliped.

Maybe I'm treating this to much in depth. Here's what he wants me to do.

Print list (array) backward. I guess doing to sections to make it look nice is just getting in the way.

This post has been edited by kkgaming: 16 Mar, 2007 - 12:30 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Print List (reverse Numbers In Array)
16 Mar, 2007 - 12:44 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(kkgaming @ 16 Mar, 2007 - 01:34 PM) *

that will reverse both sets though won't it? I just want the second part fliped.

Maybe I'm treating this to much in depth. Here's what he wants me to do.

Print list (array) backward. I guess doing to sections to make it look nice is just getting in the way.


will you please expain it in more details
here what I understood upto now

if array is : 1 2 3 4 5 6
then output should be : 1 2 3 6 5 4

is that right?

if you want to flip just the second part then you need to traverse just the half part of array something like.

for(i=0;i<=length/2;i++)
print

then

for(i=length;i>length/2;i--)
again print

same is for recursion
CODE


if(index>=length) return;
if(index<=length/2)
  printf("%d",array[index]);
recursive(array,index+1,length);
if(index>length/2)
  printf("%d",array[index]);



If you are trying to say something different then please explain it completely [more examples would help.]

This post has been edited by AmitTheInfinity: 16 Mar, 2007 - 12:50 AM
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Print List (reverse Numbers In Array)
16 Mar, 2007 - 05:46 AM
Post #5

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
1. 5
2. 4
3. 3
4. 2
5. 1


somthing like that. However I just need one row of numbers, I just added the 1-5 to make it look clean and to count. But I think I only need an array taking in numbers 1 2 3 4 5 and reverseing them to 5 4 3 2 1...so I am probably doing more then i have to.

This post has been edited by kkgaming: 16 Mar, 2007 - 07:35 AM
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Print List (reverse Numbers In Array)
16 Mar, 2007 - 09:45 AM
Post #6

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
QUOTE(kkgaming @ 16 Mar, 2007 - 06:46 AM) *

1. 5
2. 4
3. 3
4. 2
5. 1


somthing like that. However I just need one row of numbers, I just added the 1-5 to make it look clean and to count. But I think I only need an array taking in numbers 1 2 3 4 5 and reverseing them to 5 4 3 2 1...so I am probably doing more then i have to.



I have done what you said but now I am getting this:

5: 5
4: 4
3: 3
2: 2
1: 1
0: 0

I want this:

0: 5
1: 4
2: 3
3: 2
4: 1
5: 0


CODE

#include <stdio.h>

//Start of Print List Iteration Fuctions



void printListRev(int thelist[], int howmany)
{
  int i;
  for(i = howmany; i >= 0; i--)
  printf("%d: %d\n", i, thelist[i]);

}



int main()
{
  
//Print out iterative Print List
   int List[4];
   int pattern;
   int lsize=5;
   int i;
   int res;
   int x;
  
  
   for (i =0; i < lsize; i++)
   List[i] = i;
  
  
   printListRev(List, lsize);
  
  return 0;
  
  
}


This post has been edited by kkgaming: 16 Mar, 2007 - 10:59 AM
User is offlineProfile CardPM
+Quote Post

janjervy
RE: Print List (reverse Numbers In Array)
18 Mar, 2007 - 05:45 PM
Post #7

New D.I.C Head
*

Joined: 18 Mar, 2007
Posts: 2


My Contributions
kindly help me please...
im not that good in creating program using turbo c language....
i can't understand what my teacher trying us to do with our activity...
she just said that...
once she enter four number
like 8,5,6,3
the outpUt will be
8,6,5,3

she want us to do this in function type... kindly help me please i know your too smart for this.....
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Print List (reverse Numbers In Array)
20 Mar, 2007 - 11:07 AM
Post #8

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
recursion is described in this page:
http://dreamincode.net/forums/showtopic25218.htm

here is a way to print an array backwards:

CODE

int n = 4;
int a[n];

cout << "enter 4 numbers: ";
for (int i = 0; i < n; i++)
  cin >> a[i];

for(int i = (n-1); i >= 0; i--)
  cout << " " <<a[i] << " ";

cout << endl;

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month