473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

print an array in c++

how come the script below only prints out the first element of the array? how should I modify it?

// arrays as parameters
#include <iostream>
using namespace std;

void printarray (int arg[]) {


for (int n=0; n<(sizeof(arg)/sizeof(*arg)); n++)
cout << arg[n] << ", ";
cout << "\n";
}

int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray);
printarray (secondarray);
return 0;
}



Thanks for your help,

Gen
Aug 30 '07 #1
1 2169
RRick
463 Expert 256MB
sizeof(arg) = sizeof(*int) = 4
sizeof(*arg) = sizeof(int) = 4
4 / 4 = 1

Your arg[] declaration declares a int *, and sizeof has no idea how many ints you have in the array.

You'll have to use other techniques to figure out how many ints are in your arrary.


  • You can pass an extra parameter that defines the size of the arrary.
  • Put a "last" int value in the array. The problem with this is picking an int that doesn't show up in the array. This is the technique that strings use. They use a '\0' as the last character value for a string.
Aug 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: opt_inf_env | last post by:
Hello, What is a common way to print elements of a multidimensional array? I treid to do it in the straightforward way: $input_list = "Something"; print "$input_list"; And it does not work. ...
1
by: praba kar | last post by:
Dear all, In Php array_slice function base we can print array contents as per our desire eg) $a = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); $arr = array_slice($a,10,10); this function...
4
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of...
2
by: mordac | last post by:
Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int*...
12
by: Peter Lin | last post by:
Hey, I am just wondering if anyone has got any idea of setting up a new class so that you could just print like the old ways with the printer class, since I am writing a program that really...
0
by: frack78 | last post by:
I have written a binary search algorithm in java. I have a statement that when the search key is found it is printed to a terminal window. This statement when when called just keeps printing over and...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
6
by: mattmao | last post by:
Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue... The task is simple and I am about to finish it. However, I couldn't go around...
7
by: anklos | last post by:
Hi~! I met a problem on printing out a kind of hash array. i.e @array=(1,2,3,4,5,6); $array{$term}=1; diffent $i may contain diifernt $term(more than one term for each $i)
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.