• 0

[C++] Printing all the values in an array


Question

I wanted to know if it is possible to print all the values in an array. For example I have an array size of 50. But not all 50 are used. Lets say I just have 1 value stored in that array at index 1. So I want to just print that value. The problem is the other "slots" have weird values such as -858993460. Thanks for any help!

Link to comment
Share on other sites

Recommended Posts

  • 0

Use a std::vector instead. If you want to treat it like a regular array, use the address of the first element as your array. Here's a sample...

std::vector<int> vInts;
vInts.push_back(1);
vInts.push_back(2);
vInts.push_back(0);

// using a vector for iteration
for( size_t n=0, nMax=vInts.size(); n < nMax; n++ )
{
    std::cout << vInts.at(n) << std::endl;
}

// using a regular int pointer as your array
int* pInts = &vInts.at(0);
for( size_t n=0, nMax=vInts.size(); n < nMax; n++ )
{
    std::cout << pInts[n] << std::endl;
}

Link to comment
Share on other sites

  • 0
Use a std::vector instead. If you want to treat it like a regular array, use the address of the first element as your array. Here's a sample...

std::vector<int> vInts;
vInts.push_back(1);
vInts.push_back(2);
vInts.push_back(0);

// using a vector for iteration
for( size_t n=0, nMax=vInts.size(); n < nMax; n++ )
{
 ? ?std::cout << vInts.at(n) << std::endl;
}

// using a regular int pointer as your array
int* pInts = &vInts.at(0);
for( size_t n=0, nMax=vInts.size(); n < nMax; n++ )
{
 ? ?std::cout << pInts[n] << std::endl;
}

By the time I'm done readying oogle's examples I'm going to be a master at linked li:)s :)

Link to comment
Share on other sites

  • 0

Please take no offense, but if you do not know about vectors or linked lists then this may be what your after:

#include <iostream> ?	//For input/output
using namespace std;

int main()
{
	//Initializes the first four to the listed values, and the rest
	//to zero's. int Arrays will automatically initialize the rest 
                //the array to zeros if you list 1 value.
	int myArray[50] = {1, 2, 3, 4};	
	//This for loop will run for the total amount of times that
	//are equal to the size of the Array.
	for (int i = 0; i < 50; i++) {
 ?//The first if says if the ith number of the array 
 ?//is equal to 0 do not do anything. By ith, I mean
  //when i = 4, that is the same as myArray[4].
 ?if (myArray[i] == 0) {
 ?}
 ?//The else part says tells cout to print the array's contents
 ?//as long as they are not zero.
 ?else {
 ?	cout << myArray[i]; ?//Prints the correspondng number
 ? ? ? ?	//arrcoding to the ith position.
 ?}
	}
	return 0;
}

Link to comment
Share on other sites

  • 0

thanks a lot all of u.. by the way I have not learnt vectors but I want to.. I was referring to normal arrays... I will try genesi's code.. and Llama that is what I was talking bout.. trying to initialize my array to all zeros, but I guess I didn't know the technical term... genesi's code explains it well :)

Link to comment
Share on other sites

  • 0

To initialize all zeros:

int myArray[50] = {0};

One zero does em all.

as apposed to

int my Array[50] = {};

In the second example you have 50 int's filled with junk and that is why you saw: -858993460. I'm sure you know this but, if it helps as a refresher: When initializing, well anything, always assign it a value. For if you do not you then use what is in that memery slot upon compliation. As in your array's case it was filled with a meaningless value.

Edited by Genesi
Link to comment
Share on other sites

  • 0
thanks a lot all of u.. by the way I have not learnt vectors but I want to.. I was referring to normal arrays... I will try genesi's code.. and Llama that is what I was talking bout.. trying to initialize my array to all zeros, but I guess I didn't know the technical term... genesi's code explains it well :)

Thanks :)

I had a knowledgable, but strict professor. She pretty much wanted every line commented to the point that if the program worked but their were no comments, it was wrong. So it's now a habit.

Link to comment
Share on other sites

  • 0

The problem with an array-only implementation is that it will always impose an unnecessary sentinel value (e.g. 0). In other words, arrays like this break the code...

int array[50] = {1,0,-2,0,3,0};

This has six valid values, but Genesi's code will only print 3 values.

To properly implement this to handle arbitrary values, you'll need another variable to keep track of the number of valid values in the array. Increment and decrement this value as you correspondingly add or remove values.

Or use a vector. It pretty much handles all of the tracking for you.

Link to comment
Share on other sites

  • 0
The problem with an array-only implementation is that it will always impose an unnecessary sentinel value (e.g. 0). In other words, arrays like this break the code...

int array[50] = {1,0,-2,0,3,0};

This has six valid values, but Genesi's code will only print 3 values.

To properly implement this to handle arbitrary values, you'll need another variable to keep track of the number of valid values in the array. Increment and decrement this value as you correspondingly add or remove values.

Or use a vector. It pretty much handles all of the tracking for you.

Ah!, good point oogle. I'll go back to see if I can rewrite it to meet your suggestions (for fun).

Link to comment
Share on other sites

  • 0
for (int i = 0; i < 50; i++) {

//The first if says if the ith number of the array

//is equal to 0 do not do anything. By ith, I mean

//when i = 4, that is the same as myArray[4].

if (myArray == 0) {

}

//The else part says tells cout to print the array's contents

//as long as they are not zero.

else {

? cout << myArray;? //Prints the correspondng number

? ? ? ? //arrcoding to the ith position.

}

more simple is simply

for(int i=0; i< myArray.length; i++)

if(myArray)

cout << myArray;

since in C++ you can compare ints as bools (0=false; non-zero = true).

Better would be to initialize the ints to null, not 0, in case you wanted zero to be a possible value

Link to comment
Share on other sites

  • 0
more simple is simply

for(int i=0; i< myArray.length; i++)

if(myArray)

cout << myArray;

since in C++ you can compare ints as bools (0=false; non-zero = true).

Better would be to initialize the ints to null, not 0, in case you wanted zero to be a possible value

DogDagnit you took my fun away. Awsome code though :) Have to keep that in mind.

Link to comment
Share on other sites

  • 0

another fancy trick for switching between items in a 2 element array is

for(int i=0; (end condition); i=!i)

code

wherein the ending condition depends on some other factor, such as a value of one of the elements of the array.

it's a pity ms decided to go the java route with C# and eliminate all of the power of shortcuts like this. for example another thing you could do:

instead of

if (y>5)

x+=y

you could do

x+=y*(y>5)

since if y is not greater than 5, you'd be multiplying by zero

i really wish they didn't kill boolean casting in C# since I otherwise love the language :'(

Link to comment
Share on other sites

  • 0
more simple is simply

for(int i=0; i< myArray.length; i++)

if(myArray)

cout << myArray;

since in C++ you can compare ints as bools (0=false; non-zero = true).

Better would be to initialize the ints to null, not 0, in case you wanted zero to be a possible value

Isn't "myArray.length();" from the AP class? It's been awhile, but in my AP compsci class I tookm they forced us to use the AP classes instead. For strings, arrays and such. After the class, I found out how some things are really supposted to be done. Just a little confused right now. Haven't studied programming in a while.

Link to comment
Share on other sites

  • 0
another fancy trick for switching between items in a 2 element array is

for(int i=0; (end condition); i=!i)

code

wherein the ending condition depends on some other factor, such as a value of one of the elements of the array.

it's a pity ms decided to go the java route with C# and eliminate all of the power of shortcuts like this. for example another thing you could do:

instead of

if (y>5)

x+=y

you could do

x+=y*(y>5)

since if y is not greater than 5, you'd be multiplying by zero

i really wish they didn't kill boolean casting in C# since I otherwise love the language :'(

For some reason I like to use condition statement:

x += (y > 5)? 0 : y;

another one liner! Do they still have _ ? _ : _ in C#?

Link to comment
Share on other sites

  • 0
more simple is simply

for(int i=0; i< myArray.length; i++)

  if(myArray)

      cout << myArray;

since in C++ you can compare ints as bools (0=false; non-zero = true).

Better would be to initialize the ints to null, not 0, in case you wanted zero to be a possible value

Does array.length work in C++? I thought that was only Java? Or is it something in C++.net? It doesn't work with gcc.

Link to comment
Share on other sites

  • 0
x+=y*(y>5)

What the hell is that, just use this:

x += y &gt; 5 ? 1 : 0;

Not exactly easy to read, but it works (Y)

Parenthesized, it's better:

x += ((y&gt;5) ? 1 : 0);

Link to comment
Share on other sites

  • 0
What the hell is that, just use this:

x += y &gt; 5 ? 1 : 0;

Not exactly easy to read, but it works (Y)

Parenthesized, it's better:

x += ((y&gt;5) ? 1 : 0);

That doesn't quite do the same thing I don't think. Yours increments by 1 or 0, his increments by y or 0, at least if that's what he was aiming to do.

Link to comment
Share on other sites

  • 0

Ok.. so you're using a "slot" type of thing with these arrays?

You may want to look into using linked lists (std::list in STL I believe). linked lists are great if certain items in the array will be removed arbitrarily.

Link to comment
Share on other sites

  • 0

Hi darkmark327

x+=y*(y>5)

I like this approach...

I need to learn a lot of tricks like this....Can u suggest some good online resources...ebooks etc...

Thanx a lot

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.