array problem

This is a discussion on array problem within the C++ Programming forums, part of the General Programming Boards category; Greetings, I just got this email back from a teacher saying "the exercise asks you to show the values of ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    array problem

    Greetings,
    I just got this email back from a teacher saying "the exercise asks you to show the values of all the array elements. Your output is a string of numbers with no spaces. I cannot identify the values of all the array elements. Make your output readable.

    What? can you look and tell me am I wrong? I value all your input. I think its right but i am unsure of whats wrong with it?

    Code:
    #include <iostream>	//cin, cout, <<, >>
    #include <string>	// for strings
    
    using namespace std;
    
    	const int LITTLE = 6,	//Word LITTLE equals 6
    		      MEDIUM = 10,	//Word MEDIUM equals 10
    			  BIG = 128;	//Word BIG equals 128
    
    	int i, j, n = 9,	//integers with n being initalized to number 9.
    		temp, 
    		number[MEDIUM] = { 99, 33, 44, 88, 22, 11, 55, 66, 77};	//array of numbers
    		
    	char ch,	// characters
    		letterCount[BIG];	// array BIG
    
    	typedef double LittleDouble[LITTLE];	//redefined array LITTLE to LittleDouble
    
    	LittleDouble value;
    
    int main() 
    {
    	for (i = 0; i < n - 1; i++)	//initlaizes i = 0, n = 9, increase i.
    	{
    		for (j = i; j < n - 1; j++)	//initlaizes j = 0, n = 9, increase i.
    			if (number[j] > number[j + 1])
    			{
    				temp = number[j];	
    				number[j] = number[j + 1];	
    				number[j + 1] = temp;
    			}
    
    	}
    	
    	cout << "this is for array number["<< j <<"] = " << number[j] << '\n' 
    		 << "this is for array number["<< j+1 <<"] = " << number[j+1] <<'\n';
    	return 0;
    }
    Last edited by correlcj; 11-08-2002 at 06:13 PM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Obviously, you have a bad formatting, ie:

    If you have the values 1 2 45 67 123 8 in your array, they are outputted like this: 1245671238, which makes it impossible to see what numbers are in there.

    But I can't see why this would happen in your program, since you never print all elements. Perhaps you should move that cout into the loop?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thanks Magos!

    Perhaps you should move that cout into the loop?
    Can i ask why we should do that? I am curious.

    Can you please ellaborate further so i can better understand?
    Thanks!
    cj

    Last edited by correlcj; 11-08-2002 at 05:56 PM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, I assume that you want to print all elements with that cout? Then you have to place it inside a loop, otherwise it will only print the element of j:s current value (probably the last element).

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If you want to print the contents of an array (or even a vector!), then you can use a few STL tricks to do it easilly

    Code:
    #include <iostream>
    #include <algorithm>
    
    
    int main(int argc, char* argv[])
    { 
    	
    	int arr[] = {1,2,3,4,5,6};
    	
    	std::copy(arr,arr + (sizeof(arr)/sizeof(arr[0])),
    		std::ostream_iterator<int>(std::cout," "));
    }

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thanks everybody!

    That cleared up my question?
    Thanks for the help!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 03:28 PM

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