+ 3

How can i print an Array without use looping, and doing it in single out put?

6th Mar 2018, 7:52 PM
Raghad AlJabr
Raghad AlJabr - avatar
21 Answers
6th Mar 2018, 8:44 PM
Timon Paßlick
+ 17
cout<< arr[0]<<arr[1]<<arr[2]… and so on
6th Mar 2018, 8:46 PM
Mazin Ayash
Mazin Ayash - avatar
+ 9
AAAARRRGG!!!! lol @Timon you beaten me!! Anyway good job! thank you! 👌 :)
7th Mar 2018, 7:31 AM
AZTECCO
AZTECCO - avatar
+ 9
nice to help! :) this was just an introduction because often people do strange mixes between c and C++ but they are 2 different languages and troubles comes when you work on the little line between them
7th Mar 2018, 2:36 PM
AZTECCO
AZTECCO - avatar
+ 8
ok! I will, after some sleep.. you can make a thread or I will create a code for you..
7th Mar 2018, 12:45 AM
AZTECCO
AZTECCO - avatar
+ 8
Let's start from the basics.. 1st) What is a reference? A reference is an alias for an object. myClass obj; myClass& ref=obj; now ref is the same as obj. NOTE: reference variable means nothing. a variable or object occupy a region in the stack .. int a=99; int& a= 99;//whaaat?? NO! 2nd) reference is C++ c doesn't have any reference, it has only pointers. int *ptr = &someVar; do not confuse address of operator with reference 3rd) pointer is a variable which contains an address , you can change its value, its address so it points to a different obj. a reference cannot change which var it references. you should / must use references always.. use pointers only if you cannot use references. 4th) use: performance References and pointers are used to pass objects without the call of a copyconstructor and the instantiation of a new obj. yes.. every class has a copyconstructor, even if you didn't defined one. References are safer, ptr are dangerous but more powerful and flexible. 5th) a const reference means you cannot change the value of the object by reference, if you try it the compiler prevents you. use const when you don't want to change the value.. your code is more readable.. its clearly defined that you don't want your function to change the argument value. int square(const int& x){ return x * x;} if you try {x *= x; return x // error
7th Mar 2018, 12:57 PM
AZTECCO
AZTECCO - avatar
+ 8
Attention, a copy constructor is.. myclass ( const myClass&) //note the const reference deleting the copy constructor is used to prevent clients use it when you don't want. if you have a function like this. void foo ( myClass obj)// if you deleted the cpyctor it won't work.. you are forced to pass by reference void foo(myClass& obj)//ok
7th Mar 2018, 5:17 PM
AZTECCO
AZTECCO - avatar
+ 7
Not sure if that is what you are asking for but here goes: #include <stdio> #include <iterator> #include <algorithm> int my_array[5] = {1, 2, 3, 4, 5}; std::copy( std::begin(my_array), std::end(my_array), std::ostream_iterator<int>(std::cout, std::endl) ); It of course still uses a loop behind the scenes. You'll also need to know a bit about the standard library to make sense of that!
6th Mar 2018, 8:12 PM
Schindlabua
Schindlabua - avatar
+ 7
@Timon add the { after the else statement and pass an array whit at least one element and you'll be fine! Very good solution!
6th Mar 2018, 10:31 PM
AZTECCO
AZTECCO - avatar
+ 7
@rudolph this is the only structured way to pass an array to a function. You know this is not a simple task. (&arr)[5] means only an array of 5 elements is accepted, otherwise compiler will complain. it's useful if you don't want your client to pass a different size , wrong argument. the second.. template <size_t N> void too( type (&arr)[N] ) this syntax makes your function capture the size of the array you passed by reference. It's used to pass a generic size. NOTE: C++ is OOP so it's always better to use classes approach.. This means you should create a class continer or use one of the STL like vector or array!
6th Mar 2018, 10:55 PM
AZTECCO
AZTECCO - avatar
+ 7
exactly! just a thing.. I don't know but your sentence makes me think you have some gaps between pointers and references. They seems to be the same thing but they aren't. Am I wrong? if so I happily wish to help
6th Mar 2018, 11:28 PM
AZTECCO
AZTECCO - avatar
+ 6
@Rudolph what you mean?
7th Mar 2018, 6:35 PM
AZTECCO
AZTECCO - avatar
+ 6
ah ok! yes exactly! 👌
7th Mar 2018, 7:23 PM
AZTECCO
AZTECCO - avatar
+ 2
This is new with C++... maybe 11? Idk. The array is taken as a reference, but it converts implicitly to a pointer so that it is kind of the same again. But you can't pass an int[4] or int[6] now, for example. I'm working on a "real" answer code and I'm getting some weird compiler errors. Can you help me?
6th Mar 2018, 9:10 PM
Timon Paßlick
+ 2
What do you mean with "functions just by the name of the variable"? You can do Func({…}) in many cases, the name of the array outside the function scope is irrelevant for the function implementation.
6th Mar 2018, 9:50 PM
Timon Paßlick
+ 2
With a reference, you have a hidden pointer which is always dereferenced. This avoids copying overhead. But for small variables, it's faster to copy than to create the hidden pointer. type &var = other_var; var is a reference to a named variable ("lvalue reference"). If you change var, you also change other_var and vice versa. type &&var = expression; var is a reference to the result of a temporary expression which will be deleted right after the function call ("rvalue reference"). You must change both sides again, but now, that doesn't matter that much. std::move() does nothing but an rvalue reference conversion. const type &var = anything; As var is constant, anything can be passed to it. const references are just there to avoid copy overhead. I'd use them for large data types and in templates. You just can't know if someone passes something big to your template. With all references, be sure not to use a variable which is deleted. This can happen fairly easily. For example: int &unsafe_and_bad_dont_ever_ever_call() { int x{5}; return x; /*x is DELETED*/} Sorry, AZTECCO, but I was first. ( ;
7th Mar 2018, 7:12 AM
Timon Paßlick
+ 2
I totally support AZTECCOs opinion that a std::array<T, N> should be used. I just wanted to challenge myself with the c style array approach. And we've all learned something by that. I learned that compilers can be strange.
7th Mar 2018, 7:23 AM
Timon Paßlick
+ 2
( :
7th Mar 2018, 7:35 AM
Timon Paßlick
7th Mar 2018, 11:04 AM
Timon Paßlick
+ 2
Edited: You can delete the copy constructor: class C { C(const C&) = delete; };
7th Mar 2018, 3:13 PM
Timon Paßlick