954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

arrays and output

hi everyone

for my comp sci class i'm supposed to write a piece of code which will take in 10 ints and put them into an array of such size. unless the input is either a 3 or 7 in which case wats left of the array will be written with either number. i'm only havin trouble with printing out the array with the desired values.

i thought of using a for loop to obtain the values,
then a while loop to check wheter it is a seven or a three
fill in whats left of it with the respective numbers
and output it

the problem is that i am stuck with the cout << command because all i get when i run it is the address and if not able to figure out wats wrong at this point, i dont know how to figure out if my program is working correctly

PS: i think i comment too much

// THIS PROGRAM will take 10 numbers from user and put them into an array
//      if any of them is a 3 then the rest of the array will be filled with 3's
//		if any of them is a 7 then the what is left of the array will be filled 7's

/**PROCEDURE
 * (a) declare an integer array of size 10, and 
 *		(a1)  ask user to enter up to 10 integers and 
 *		(a2)  put them into the array
 * (b) if a 3 is ever entered, fill what is left with 3's and the program will stop
 * (c) if a 7 is ever entered, fill what is left with 7's and the program will stop
 **/
 
#include <iostream>

using namespace std;

int main(){

int data[10]; //(a)
int value=0;

//for(int i=0; i<10; i++){

	
	for(int i=0; i<10; i++){
		cout << "please enter up to ten integers: ";
		cin >> value;
		data[i] = value;
			//while(value!=3){
			
			//}
	}
	
	
	cout<<data;
return 0;
}
resduq
Newbie Poster
6 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

cout << data[index]

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 
cout << data[index]


i am actually trying to print out the whole array no just a value

resduq
Newbie Poster
6 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

You cannot print the whole array in one action. You cannot input a whole array in one action. There's really not much at all you can do with an array as a whole.

You have to display it, element by element, in the same fashion as your input loop.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

You cannot print the whole array in one action. You cannot input a whole array in one action. There's really not much at all you can do with an array as a whole.

You have to display it, element by element, in the same fashion as your input loop.

is it possible in any way to print out the whole data???

resduq
Newbie Poster
6 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

I dont think so. just put the loop and print each element. however you can format the output in any way, everything in 1 line, separate lines, tabs etc.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

Read again what I wrote.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

>You cannot print the whole array in one action.
Sure you can:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

template <typename T, std::size_t N>
T *end ( T (&array)[N] ) { return array + N; }

int main()
{
  int a[] = {1,2,3,4,5,6,7,8,9,0};

  copy ( a, end ( a ), std::ostream_iterator<int> ( std::cout, "\n" ) );
}

>You cannot input a whole array in one action.
Sure you can:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

int main()
{
  int a[10];
  int *end;

  // Unsafe
  end = copy (
    std::istream_iterator<int> ( std::cin ),
    std::istream_iterator<int>(),
    a );
  copy ( a, end, std::ostream_iterator<int> ( std::cout, "\n" ) );
}

That's not a particularly good idea though, as it's effectively an unbounded loop, so you've got the risk of buffer overflow on your array:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

int main()
{
  int a[10];
  int *end;

  end = a;

  // Unsafe
  while ( std::cin>> *end )
    ++end;

  copy ( a, end, std::ostream_iterator<int> ( std::cout, "\n" ) );
}

However, you can write your own copy_n algorithm and using it still counts as one action:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

namespace jsw {
  template <typename In, typename Out, typename Count>
  Out copy_n ( In first, In last, Count n, Out dst )
  {
    while ( n-- > 0 && first != last )
      *dst++ = *first++;

    return dst;
  }
}

int main()
{
  int a[10];
  int *end;

  end = jsw::copy_n (
    std::istream_iterator<int> ( std::cin ),
    std::istream_iterator<int>(),
    sizeof a / sizeof *a,
    a );

  copy ( a, end, std::ostream_iterator<int> ( std::cout, "\n" ) );
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>You cannot print the whole array in one action. Sure you can:


Let me rephrase that.

In the context of what's learned so far in a first term C++ course, there is no simple way to read in or display an array in one action.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

cool...

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 
for (int i =0; i < 10;i++)
      cout << data[i] << " ";


the space is there for clarity, but you can take it out.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

sorry i had posted on the wrong thread

resduq
Newbie Poster
6 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

let me rephrase what i had said before... i do not really need to print the whole array in one step/action..... i just needed it to appear in one line.... i realized that it wasnt happenin because an arrays name is just the address of it.... so i just wrote another separate for loop to make it happen


thanks everyone i appreciate the help

resduq
Newbie Poster
6 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You