Zeus
This is the main question.. I was given a static code (header snippet and main function)
Zeus
Here is the header snippet
Zeus
Here is the main function..
Zeus
My question is... How do I use the pointer ( *ptr) declared in the class sorting
Zeus
To address the array I want to store my elements in
Zeus
I didn't know what to do so I just made an array of my own and did this :/
Roxifλsz 🇱🇹
First thing is, that your function signatures should contain names for your arguments, for example: sorting::read(int* input)
Anonymous
//Program to Convert binary no to decimal #include<stdio.h> #include<math.h> void main() { int i,n,p=0,l,k,c,t=0,s; printf("Enter any binary no :\n"); scanf("%d",&n); while(n!=0) { i=n%10; p=p+1; n=n/10; } //printf("\nThe no of digits entered : %d",p); c=t; s=pow(2,l); for(l=0;l<=p-1;l++) { k=n%10; c=c+(k*s); n=n/10; } printf("\ndecimal = %d",c); }
Anonymous
anyone please help me with this
Roxifλsz 🇱🇹
I didn't know what to do so I just made an array of my own and did this :/
You should also be more consistent with your code style, to make it more readable. Also the next time you send photos, please make sure they're oriented correctly, I wouldn't want to break my head off by looking at your code photos
Roxifλsz 🇱🇹
Here.. "sort" is the class name right?
Yes, I edited the example to be correct
Mat
anyone please help me with this
No without at least a question.
olli
Here.. "sort" is the class name right?
sorting is, sort is the name of the function
Anonymous
No without at least a question.
It's a program to convert binary no. To decimal
Roxifλsz 🇱🇹
It's a program to convert binary no. To decimal
Well you should tell us what the problem is exactly
Zeus
sorting is, sort is the name of the function
But aren't we supposed to call a function like void class_name :: function() {}
Zeus
Yes, I edited the example to be correct
Alright so I should have wrote :- void sorting :: read(int* arr)?
Roxifλsz 🇱🇹
But aren't we supposed to call a function like void class_name :: function() {}
That's member function definition, classInstanceName.functionName(arguments); would be the function call
Roxifλsz 🇱🇹
Anonymous
Try to analyze things like that
Anonymous
Life will be way better
Zeus
That's member function definition, classInstanceName.functionName(arguments); would be the function call
Ooo even if a function is not a member.. and is defined like class A { Func(); }; A.Func(blah blah); ??
Zeus
Yeah, and similarly in the other function
So should I create another array in read function??? To take input for the elements I want to sort?
Roxifλsz 🇱🇹
So should I create another array in read function??? To take input for the elements I want to sort?
Well, your int* arr is the input, and if it's an array it's values can be accessed using arr[position]
Roxifλsz 🇱🇹
Although I'm having a hard time grasping what should each function exactly do in your program, I'll look trough the photos again
Roxifλsz 🇱🇹
How do I call the same array in the sort function as well?
Seems like the main() function in your photo should already pass the same array with values received from read() to sort()
Anonymous
thanks
klimi
What
klimi
O.o
Roxifλsz 🇱🇹
So what exactly the array name should be? arr[5]??
I meant to say, that giving the array to the sort function should already work in your program
MᏫᎻᎯᎷᎷᎬᎠ
.
Zeus
I meant to say, that giving the array to the sort function should already work in your program
O I see.. although I tried reading the input the elements in my own defined array.. But somehow the sort function couldnt read it... I guess my syntax was wrong.. I should have done void sorting :: read(int *arr[5]) { for( int i;i<=5;I++) cin >> arr[i] } void sorting :: sort(int* arr[5]) { rest of the code using arr[5]; }
Zeus
You should use int* arr instead of int* arr[5] and your for loop is not correct, it should be for(int i = 0 ; i < 5 ; i++)
But I want arr to be an array.. is it okay to declare it like int* arr?? And the max number elements should be 5 that's why I used i<=5 instead of i<5
Roxifλsz 🇱🇹
But I want arr to be an array.. is it okay to declare it like int* arr?? And the max number elements should be 5 that's why I used i<=5 instead of i<5
An array variable really is just a pointer, therefore it should be int* arr, altough int arr[] might also work, I don't remember, also array members start from 0, so the i < 5 is correct
Zeus
An array variable really is just a pointer, therefore it should be int* arr, altough int arr[] might also work, I don't remember, also array members start from 0, so the i < 5 is correct
Isn't the arr[0] the position? if I used i<=5 it won't hurt right? Cuz even if there is a 6th element.. then it's automatically gonna be 0
Roxifλsz 🇱🇹
Isn't the arr[0] the position? if I used i<=5 it won't hurt right? Cuz even if there is a 6th element.. then it's automatically gonna be 0
arr[0] accesses the first element of the array, if you use i <= 5 then you also have to do int i = 1 in your for loop declaration and arr[i-1] for accessing array elements
Roxifλsz 🇱🇹
In most cases it really is simpler to just get used to arrays starting at 0
Zeus
arr[0] accesses the first element of the array, if you use i <= 5 then you also have to do int i = 1 in your for loop declaration and arr[i-1] for accessing array elements
Ooooo!!! Gotcha!! Thank u so much! I'll try it tomorrow! I hope it runs successfully.. I have been stuck at it for hours.. Moreover i tried running it on other ide and when I inputs any random 5 digits it just gets stuck or something idk why
Zeus
And there's still one thing that's been bugging me.. If I need to out put the sorted elements.. should I be applying a loop for the Sorted[j] or sorted[i]?
Zeus
And how can I print them in the display function?
Roxifλsz 🇱🇹
And how can I print them in the display function?
for(int i = 0 ; i < 5 ; i++) { std::cout << arr[i] << '\n'; }
Zeus
for(int i = 0 ; i < 5 ; i++) { std::cout << arr[i] << '\n'; }
Ooo alrighty!! I see all the sorted elements r already into arr.. and what should I do with that *ptr pointer that's been declared in the class sorting for god knows what reason?
Roxifλsz 🇱🇹
Ooo alrighty!! I see all the sorted elements r already into arr.. and what should I do with that *ptr pointer that's been declared in the class sorting for god knows what reason?
You could make all the member functions use that pointer instead of giving the array to them trough arguments or ignore/delete it, depends on what the expectation of the task is
Zeus
You could make all the member functions use that pointer instead of giving the array to them trough arguments or ignore/delete it, depends on what the expectation of the task is
That's what I have been asking... How to do that?😭😂 Should I use the same pointer for all the elements in the array? Or can I call the whole array with a pointer?
Roxifλsz 🇱🇹
That's what I have been asking... How to do that?😭😂 Should I use the same pointer for all the elements in the array? Or can I call the whole array with a pointer?
That requires some refactoring of your code... The functions should just use ptr instead of arr, your main function shouldn't have an array at all, the new call should be used on the ptr in the class
Roxifλsz 🇱🇹
But the main function contains an array! That's been bugging me this whole time!
It shouldn't contain an array, if you want to use ptr from inside the class
Zeus
It shouldn't contain an array, if you want to use ptr from inside the class
Then even if am using the arr.. why is there a *ptr declared in the first place? Am kinda confused on this one..
Zeus
Is the static code wrong?
Roxifλsz 🇱🇹
Zeus
Possibly
I knew it.. its gotta be.. and one thing that still doesn't explains the segmentation error am facing :/
Roxifλsz 🇱🇹
I knew it.. its gotta be.. and one thing that still doesn't explains the segmentation error am facing :/
Probably an uninitialized variable somewhere is the cause of the segfault
Zeus
Probably an uninitialized variable somewhere is the cause of the segfault
I tried running the same code in other ide and it worked.. I mean the code did not but therr was no error... No segfault at all
Zeus
Maybe am using more lines of code over the limit...
Ludovic 'Archivist'
Thanks for the comments full of hatred . I hope people should try to empathize with beginners rather than expecting miracle from them.
Do not take such a phrasing against yourself. When you are in a place that is filled with people who are expert or experienced people, consider they are just bluntly honest because they are trying to show where is the place there is the most room for improvement.
Ludovic 'Archivist'
Thanks for the comments full of hatred . I hope people should try to empathize with beginners rather than expecting miracle from them.
Ugly code is often the announcement of poor rigor when programming, and is generally the root of lots of evils. Ugly code hides ideas and logic, it makes finding logic errors harder and understanding the code you shared tricky for people who want to help you
Mat
Thanks for the comments full of hatred . I hope people should try to empathize with beginners rather than expecting miracle from them.
It's the truth. Do indentation, use more lines, use empty lines, make the code readable. And explain your desired output
MᏫᎻᎯᎷᎷᎬᎠ
Guys
Ludovic 'Archivist'
Thanks for the comments full of hatred . I hope people should try to empathize with beginners rather than expecting miracle from them.
You may think that fixing indentation will not solve your problem, but it may make you think about what you meant and what you wrote and the distance in between
MᏫᎻᎯᎷᎷᎬᎠ
Newbies = ugly code
MᏫᎻᎯᎷᎷᎬᎠ
It's just the beginning of his road
MᏫᎻᎯᎷᎷᎬᎠ
He'll get much more better
Ludovic 'Archivist'
It's just the beginning of his road
And that is why I explain why it is so important
MᏫᎻᎯᎷᎷᎬᎠ
I hope so
Mat
Newbies = ugly code
I considered something like that ugly even before i started programming