+ 2
How to improve my code
I have a piece of code which I want to see in what ways I can improve and what I should do differently to make it more "professional" #include <iostream> using namespace std; void continuemo(); int myArr[8]; int main() { for(int x = 0; x<8; x++){ cin >> myArr[x]; } continuemo(); } void continuemo(){ for(int x = 0; x<8; x++){ cout << myArr[x] << endl; } }
12 Respostas
+ 10
#include <iostream>
using namespace std;
void continuemo(int[]);
int main()
{
int myArr[8];
for(int x = 0; x<8; x++)
{
cin >> myArr[x];
}
continuemo(myArr);
return 0;
}
void continuemo(int myArr[])
{
for(int x = 0; x<8; x++)
{
cout << myArr[x] << endl;
}
}
+ 10
@~datell Good points you have there, but since vectors are not covered in SL, I would have a way to do it without them.
#include <iostream>
using namespace std;
int y;
void func();
int main()
{
int x;
cin >> x;
int array[x];
y = sizeof(array)/sizeof(array[0]);
func();
return 0;
}
void func()
{
cout << y;
}
As much as I dislike global variables, this is one way to get size of array without passing it into the function which needs it. Thanks for the info on sizeof() working only in one function, I did not know that. ;>
+ 8
@~datell Are you referring to: "...but since vectors are not covered in SL..."
I think @Hatsy Rei means SoloLearn, not the C++ Standard Library :)
+ 7
@~datell There is a function, sizeof() which will be able to handle the problem without so much fuzz.
+ 7
Derived from my previous post, this is an even simpler version which doesn't require the use of sizeof() lol.
#include <iostream>
using namespace std;
int y;
void func();
int main()
{
cin >> y;
int array[y];
func();
return 0;
}
void func()
{
cout << y;
}
+ 5
thank you everyone for your answers, specially you Hatsy Rei, I had forgotten about passing values through parameters
+ 4
Maybe replace x++ with ++x. It could make your code that tiny bit faster đ
+ 1
The post that got so heavily upvotes is a good example. What do you do, if your array doesn't have 8 entries but a variable number that depends on the users input. How long will you loop? The user will have to specify the length of the array and you have to pass the length as a parameter in your function. This is pure C style and nothing you have to use in C++ sonce there are much superior things available.
+ 1
@Hatsy Rei: Nope! You can get the size of an array with sizeof(array)/sizeof(array[0]). But this only works within one function. Try it out! Create a function test(int array[]) and pass an array try to get its size in your function test. This doesn't work. The reason is that not really an array is passed but a pointer to the array[0] that points towards the beginn of the allocated memory section.
EDIT: Maybe this article could be help you guys. Bjarne Stoustrup who invented C++ on C style arrays:
http://www.stroustrup.com/bs_faq2.html#arrays
+ 1
@Jafca: Oh haha I think you are right! Sorry then!
0
1. Use english names! This is a must in programming!
2. Although even SoloLearn does it, it is acutally a bad habbit to use using namespace. If you work in bigger projects this can heavily confuse you and others. Better just type the 5 more letters std::
3. Avoid using C arrays! They are evil since they are not aware of their own size! Better use std::vector (#include <vector>) or if you statically now the size at the initialization point use std::array (#include <array>). Google these, not a single serious C++ developer will use C arrays, maybe in very very low level applications but in no other case.
0
@Hatsy Rei: Sorry but you are again wrong. std::vector is part of the C++ SL (see here: http://www.cplusplus.com/reference/) and NOT of the STL.