Arrary & ForLoop

This is a discussion on Arrary & ForLoop within the C++ Programming forums, part of the General Programming Boards category; I'm trying to do a program that will take in 10 numbers (integers) then have the out given in reverse. ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    24

    Arrary & ForLoop

    I'm trying to do a program that will take in 10 numbers (integers) then have the out given in reverse. Ex: 1,5,9 (in) Out --> 9,5,1

    Here is my code so far:

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    const int LIMIT = 15;
    
    int main()
    {
    	int counter;
    	int sum; 
    	int dataValue; 
    	counter= 1;
    	sum = 0;
    
     
    
      while (counter<=15)
      {
      cout<<"Enter an integer value. Press return."
      <<endl;
      cin>>dataValue;
      sum = sum + dataValue;
      counter++;
      }
    
      cout<<"Numbersreversed"<<sum<<endl;
    
      return 0;
    }

  2. #2
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Im still really new here, but must the last cout statement be outside the while?

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,409
    You've not stored the input. (Summing the input doesn't count.) See any of the STL containers you like.

    Soma

  4. #4
    Village id10t
    Join Date
    May 2008
    Posts
    57
    oh ok, now even i get it... sum=sum+datavalue only adds all the inputs...

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by slimdime View Post
    I'm trying to do a program that will take in 10 numbers (integers) then have the out given in reverse. Ex: 1,5,9 (in) Out --> 9,5,1
    Your code doesn't seem to have been written for this assignment.

    [edit]
    Look. It is for adding numbers, not to print in reverse order

    [/edit]

    What you need is an array
    Code:
    int num[10];
    Last edited by abh!shek; 05-15-2008 at 01:48 PM.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    24
    Yea, I know it's for "summing" I'm trying to change it to an Array with ForLoop to print reverse.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    oh, you already knew its for adding!

    ok then do it with arrays and for loop. Good luck

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    The point of declaring constants like LIMIT is so they can be used, not ignored.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    24
    Quote Originally Posted by abh!shek View Post
    oh, you already knew its for adding!

    ok then do it with arrays and for loop. Good luck
    Can you give me a hint?

  10. #10
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Code:
    int array[15]
    
    for (counter=0;counter=<15;counter++)
    {
    cout<<"please enter a number"<<endl;
    cin<<array[counter];
    }
    or something like that, never take my word as gospel

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by slimdime View Post
    Can you give me a hint?
    What MarlonDean said. But its "cin>>"

    After getting the input, you make another for loop to print the output but this time counter will be initialized to 15 (or whatever the upper limit is) and will decrement by one with every iteration.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    24
    I tried it: (but it didn't work) Thanks for trying.

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    const int LIMIT = 15;
    
    int main()
    {
     int array[15]
    
    for (counter=0;counter=<15;counter++)
    {
    cout<<"please enter a number"<<endl;
    cin>>array[counter];
    }	
    
      return 0;
    }
    Last edited by slimdime; 05-15-2008 at 02:51 PM.

  13. #13
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Get rid of the line "const int LIMIT = 15;" (or keep it and use it in the for loop)

    As I said in the earlier post, it is cin>> not cin<<

    and you need a second for loop to print the output.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    24
    I made the change cin>>

    Isn't this my ForLoop

    Code:
    for (counter=0;counter=<15;counter++)

  15. #15
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by slimdime View Post
    I made the change cin>>
    ok
    Isn't this my ForLoop

    Code:
    for (counter=0;counter=<15;counter++)
    uh? No this is MarlonDean's for loop. He wrote this in post #10


    Also write a for loop which will count backwards from 14 to 0 and put a cout statement in it.
    Last edited by abh!shek; 05-15-2008 at 03:11 PM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-09-2009, 07:17 PM
  2. Arrary with user defined size
    By GCNDoug in forum C++ Programming
    Replies: 8
    Last Post: 04-09-2008, 05:34 PM
  3. Arrary Size
    By Hexxx in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2006, 01:21 PM
  4. Abot Arrary Sorting
    By FeNCinGeR in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2006, 09:39 AM
  5. Returning a non temporary arrary from function
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2002, 07:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21