0

im a new learner and i have a simple question.

hi everyone i just have a simple question about my code... i gonna ask on this part in my own output wich is :(1,2,3,4,5,6,7,8,9,10,). what to do to remove the last ","? im a new learner so my code is simple sorry. xD and if u guys can give me advices to improve the code or givin me general tips then... please DO!.thanks. btw that's my simple code: https://code.sololearn.com/cM5OcwHs0U70/#cpp

4th Aug 2018, 3:36 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar
11 Respostas
+ 3
int a[]={1,2,3,4,5,6,7,8,9,10}; int size = sizeof(a) / sizeof(a[0]); cout<<"the elements are:-\n"; for (int x=0; x<size; x++) { cout <<a[x]<< (x < size-1? "," : "\n"); }
4th Aug 2018, 3:51 PM
Babak
Babak - avatar
+ 3
Ặňặ Ǯǿmry MặƧđař Ặnśậk I guess you have trouble understanding the array size calculation (pointer arithmetic) and ternary conditional operator, right?
5th Aug 2018, 2:58 PM
Babak
Babak - avatar
+ 3
This line int *p = new int; allocates 4 bytes of the heap space to the program (a.k.a dynamic allocation). From this point, the pointer p is able to access to that 4 bytes of the memory and do calculation and other stuff and finally must be reclaimed by delete(p). But that's not the point of my earlier question. int size = sizeof(a) / sizeof(a[0]); Here, a holds the base address of the whole array. It acts as a pointer to the first element of the array. So, by making a query of the size of a, you get the whole size of the array as byte. Since the array has integer type, the sizeof gives you total number of items * size of integer type = 10 * 4 = 40 Then, to get the actual number of items you can simply divide the result by the size of a single integer or the first element of the array as 40 / sizeof(int) or 40 / sizeof(a[0])
5th Aug 2018, 3:42 PM
Babak
Babak - avatar
+ 3
Ặňặ Ǯǿmry MặƧđař Ặnśậk " can u help me to understand your code? or give me a vid or the name of this lesaon? it works but i didnt learn yet how to do that. " You didn't mention anything about your problem. you just vaguely jumped into some topics related to dynamic memory allocation. Anyway. I'm short in time right now, so I refer you to cplusplus portal. The post explains it in more details. http://www.cplusplus.com/forum/articles/14631/
5th Aug 2018, 4:17 PM
Babak
Babak - avatar
+ 1
add a simple if statement before cout a[x] for (int x=0; x<10; x++){ if(x<9){ cout <<a[x]<<"," ; } else{cout <<a[x];} }
4th Aug 2018, 3:47 PM
Tomer Sim
Tomer Sim - avatar
+ 1
C++ Soldier (Babak) thnx i gonna try that soo as possible ^^
4th Aug 2018, 4:02 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar
0
Tomer Sim thank you i though in that but i wasn't sure 😅
4th Aug 2018, 3:49 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar
0
C++ Soldier (Babak) can u help me to understand your code? or give me a vid or the name of this lesaon? it works but i didnt learn yet how to do that..
5th Aug 2018, 2:44 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar
0
C++ Soldier (Babak) to be honest im not sure 100% i mean i dont have ideas to practice😅 but i understand the pointer and stack and the heap i gonna tell you how i understood it and current me if im wrong, for example: int *p= new int *p=5 the p itself its the code (idk what's the word exactly in english srry, tell me the right names if u can) that code its like exist in the stack and the "*" itself points to the value of it which is located in the heap which is 5. i hope im right XD
5th Aug 2018, 3:07 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar
0
C++ Soldier (Babak) noo bro i understand this i just was asking about this part (x<size-1?",":"\n"); cause i didnt learn it yet, and btw thanks for explaining the heap i got some new informations ^^.
5th Aug 2018, 3:59 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar
0
C++ Soldier (Babak) srry dude my bad i be dumb sometimes 😅 , and thanks a lot
5th Aug 2018, 4:24 PM
Ặňặ Ǯǿmry MặƧđař Ặnśậk
Ặňặ Ǯǿmry MặƧđař Ặnśậk - avatar