Iakovos
for example if you run it for 5 elements array e.g. 50 40 30 20 10 I will get 14 comparisons when the correct is 10
Iakovos
your algorithm yields 10 if you remove the increment before the while loop.
yeah I know that as well but if you run it then for 5 elements again but with this order: 10 20 30 40 50 I will get 0(wrong answer) and not 4(correct answer)
olli
yeah I know that as well but if you run it then for 5 elements again but with this order: 10 20 30 40 50 I will get 0(wrong answer) and not 4(correct answer)
yes, but is 10 really the correct result? what should be the result for [50, 40] ? 10 is the correct result for number of "swaps" - not comparisons, as is 0 for the number of swaps in an already sorted array.
Iakovos
also yeah 10 is the correct number of comparisons, check here: http://138.47.18.129/book/algorithms/algorithmsSorting2.html
olli
I need the comparisons number not swaps, so what did I do wrong really?
I see, okay. In the case where you need to swap you count the first comparison twice. You could change it by having a comp function that increments the counter or only incrementing in case the while loop does not execute. The first option could be inline int comp(int x, int y, int * c) { *c += 1; return x < y; } void insertionSort(unsigned int *arr, int n) { int i,j,x,t; int comparisons=0; for (i=1;i<n;i++) { x = arr[i]; j = i-1; while (j>=0 && comp(x, arr[j], &comparisons)) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = x; } printf("Comparisons: %d\n",comparisons); }
Iakovos
Yeahh! That's the correct approach, I didn't really know about that comp function really grateful to you man. I really appreciate it thank you a lot!🙂
Iakovos
Iakovos
the first option works no matter what, but the second when you give it random numbers it counts less
Iakovos
https://media.cheggcdn.com/media%2F61c%2F61c35d19-d051-403a-8d32-5345d4554640%2FphpiamcWQ.png For example if you check this instance it should be 15.
olli
https://media.cheggcdn.com/media%2F61c%2F61c35d19-d051-403a-8d32-5345d4554640%2FphpiamcWQ.png For example if you check this instance it should be 15.
I guess just use the counting comparison function or this - which should yield the same result while (j>=0 && x < arr[j]) { arr[j+1] = arr[j]; j = j-1; ++comparisons; } if (j>=0 && !(x < arr[j])) { ++comparisons; } arr[j+1] = x;
Al
Hello guys! I ran into a problem trying to count comparisons of insertion sort in an array. https://pastebin.pl/view/dbb9f1b1 Any help is much appreciated!
malloc the array AFTER the scanf or the result is unexpcted.... your array doesnt know how many elements UNTIL you scanf
Al
the array is supposed to be dynamic, but you never assign an array dinamically
Ayush
S C:\Users\ayush\OneDrive\Desktop\C Lang> cd "c:\Users\ayush\OneDrive\Desktop\C Lang\" ; if ($?) { gcc trail.c -o trail } ; if ($?) { .\trail } Program 'trail.exe' failed to run: Access is deniedAt line:1 char:93 + ... top\C Lang\" ; if ($?) { gcc trail.c -o trail } ; if ($?) { .\trail } + ~~~~~~~. At line:1 char:93 + ... top\C Lang\" ; if ($?) { gcc trail.c -o trail } ; if ($?) { .\trail } + ~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException + FullyQualifiedErrorId : NativeCommandFailed
Wojak
/get bro
Manish
Is it possible for unique pointer to being "a" null here ? A a; A b=std::move(a); // a become null here
olli
Is it possible for unique pointer to being "a" null here ? A a; A b=std::move(a); // a become null here
If A is an alias of a unique_ptr this is guaranteed For unique_ptr(unique_ptr&& u) noexcept; Postconditions: [...] u.get() == nullptr [...] For unique_ptr& operator=(unique_ptr&& u) noexcept; Postconditions: u.get() == nullptr. http://eel.is/c++draft/unique.ptr#single.ctor-20 http://eel.is/c++draft/unique.ptr#single.asgn-4
Manish
Thanks! Is there any way while transferring ownership to "b" a != nullptr
olli
Thanks! Is there any way while transferring ownership to "b" a != nullptr
then it wouldn't be "Transferring" but "copying" If you need multiple "owners" use std::shared_ptr Theoretically, you could extract the raw pointer and create a second unique_ptr from it - but this is asking for trouble, so don't
Afosa
/*MATRIX ARITHMETIC*/ #include <stdio.h> #include "conio.h" int main(int argv,char argc[]) { int x = 0; int y = 0; int i = 0; int j = 0; printf("MATRIX ARITHMETIC\n"); printf("Enter array dimension for the array row: "); scanf("%d", &x); printf("Enter array dimension for the array colum: "); scanf("%d", &y); int a[x][y]; int b[x][y]; int ad[x][y]; int mu[x][y]; //int di[x][y]; int su[x][y]; printf("Enter values for matrix '\A\': \n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("Array[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } } printf("Enter values for matrix \'B\': \n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]); } } printf("Matrix Addition\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { ad[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", ad[i][j]); } printf("\n"); } printf("Matrix Multiplication\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { mu[i][j]=a[i][j]*b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", mu[i][j]); } printf("\n"); } /*printf("Matrix Division\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { di[i][j]=a[i][j]/b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d", di[i][j]); } printf("\n"); }*/ printf("Matrix Subtraction\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { su[i][j]=a[i][j]-b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", su[i][j]); } printf("\n"); } getch(); return 0; }
Afosa
Criticism are welcome.
klimi
Criticism are welcome.
Please learn how to paste your code, just seeing this on Christmas makes me want to puke...
olli
/*MATRIX ARITHMETIC*/ #include <stdio.h> #include "conio.h" int main(int argv,char argc[]) { int x = 0; int y = 0; int i = 0; int j = 0; printf("MATRIX ARITHMETIC\n"); printf("Enter array dimension for the array row: "); scanf("%d", &x); printf("Enter array dimension for the array colum: "); scanf("%d", &y); int a[x][y]; int b[x][y]; int ad[x][y]; int mu[x][y]; //int di[x][y]; int su[x][y]; printf("Enter values for matrix '\A\': \n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("Array[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } } printf("Enter values for matrix \'B\': \n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]); } } printf("Matrix Addition\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { ad[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", ad[i][j]); } printf("\n"); } printf("Matrix Multiplication\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { mu[i][j]=a[i][j]*b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", mu[i][j]); } printf("\n"); } /*printf("Matrix Division\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { di[i][j]=a[i][j]/b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d", di[i][j]); } printf("\n"); }*/ printf("Matrix Subtraction\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { su[i][j]=a[i][j]-b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", su[i][j]); } printf("\n"); } getch(); return 0; }
- VLA are optional and will impact your programs stability - I don't see any reason to use conio - commenting out code is pretty bad - you don't check for invalid inputs
Abid
How i can solve this problem g++ is not recognaized in visual studio i am using widose 8.1
Abid
Please help me
Anonymous
is it any errors?
Anonymous
you may need to try changing the scanf to scanf_s
Igor🇺🇦
How i can solve this problem g++ is not recognaized in visual studio i am using widose 8.1
Visual Studio has integrating compiler. No need to use gcc. Windows 8 is not supported. Why are you still on using it?
Igor🇺🇦
/*MATRIX ARITHMETIC*/ #include <stdio.h> #include "conio.h" int main(int argv,char argc[]) { int x = 0; int y = 0; int i = 0; int j = 0; printf("MATRIX ARITHMETIC\n"); printf("Enter array dimension for the array row: "); scanf("%d", &x); printf("Enter array dimension for the array colum: "); scanf("%d", &y); int a[x][y]; int b[x][y]; int ad[x][y]; int mu[x][y]; //int di[x][y]; int su[x][y]; printf("Enter values for matrix '\A\': \n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("Array[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } } printf("Enter values for matrix \'B\': \n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]); } } printf("Matrix Addition\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { ad[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", ad[i][j]); } printf("\n"); } printf("Matrix Multiplication\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { mu[i][j]=a[i][j]*b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", mu[i][j]); } printf("\n"); } /*printf("Matrix Division\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { di[i][j]=a[i][j]/b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d", di[i][j]); } printf("\n"); }*/ printf("Matrix Subtraction\n"); for(i=0;i<x;i++) { for(j=0;j<y;j++) { su[i][j]=a[i][j]-b[i][j]; } } for(i=0;i<x;i++) { for(j=0;j<y;j++) { printf("%d\t", su[i][j]); } printf("\n"); } getch(); return 0; }
In addition to comments above there is a lot of code that repeats itself. Extract repeating parts to functions and use them instead.
Anonymous
AOA all of you
ninja
hi there , I am a collage student and want to start in open source .But don't know how to start(i know about git but don't know what to do)
Alex
hi. look through github and try to find issues or close existing
ninja
can you recommend any repo for this
Alex
find popular project in the area you want to work
ninja
mean ,i love c++ and can work in this
Pavel
mean ,i love c++ and can work in this
There are like thousands of projects on C++. You need to pick some areas you're interested in. Here are some random examples of projects: https://github.com/MunGell/awesome-for-beginners#c-2
ninja
thankyou
Al
hello according to this ...Spamming/advertising (job posts are also ads, so ask an admin before posting) will grant you a warning or an immediate ban if you do it right after joining... i have a Gig on fiverr about a C course , so i would like to ask an admin if i can post the link here .. thanks in advance and Happy Christmas
Hasem
Hii i am look friend who is the pro at C
klimi
Hii i am look friend who is the pro at C
Oh do you have some interesting project?
Hasem
İ have homework project whic name is airline reservation sytems
klimi
İ have homework project whic name is airline reservation sytems
Oh so youre just searching for someone who could do your homework
Hasem
Yes
klimi
I see
Apk
Yes
And how much are you paying for it?
Yuval
Yes
If you want someone to do your homework put a price on it. We happy to help you never the less just we don't want to waste our time
Anonymous
AOA
Anonymous
i want to paste a screen shot then want to ask a question but there is no option to paste now what i do??
Anonymous
anyone tell me please i want to aske a question of C++
Pavel
i want to paste a screen shot then want to ask a question but there is no option to paste now what i do??
If you want to past a screenshot of your code past the code using pastebin instead
Anonymous
"postind media content cant alowed" it is showen
Pavel
Because pasting photos of code can make you warned for rules violation
Anonymous
whats mean pastebin???
Pavel
whats mean pastebin???
https://pastebin.com/ There are other services like this if this one doesn't work to you (e.g. if you're in Turkey) E.g. https://hastebin.com/
Anonymous
#include<iostream> using namespace std; int main() { //I want to implement the program which take 2 numbers from user //then take its square and then add these twao numbers //please tell me what should i do?? int num1; int square cout<<"please enter a number"<<endl; cin>>num1; square = num1 *num1; cout<<" square of a number="<<square<<endl; int num2; int square; cout<<"please enter a number"<<endl; cin>>num2; square = num2 *num2; cout<<" square of a number="<<square<<endl; } }
Anonymous
yupp😍
Pavel
you can either do that right away cout << "sum of squares=" << (num1 * num1) + (num2 * num2) or store two variables instead of one square, like square1 and square2 and then do (square1 + square2).
Anonymous
😊
Nils
just tried it bro, its seconds
It's platform dependent
Pavel
#include<iostream> using namespace std; int main() { //I want to implement the program which take 2 numbers from user //then take its square and then add these twao numbers //please tell me what should i do?? int num1; int square cout<<"please enter a number"<<endl; cin>>num1; square = num1 *num1; cout<<" square of a number="<<square<<endl; int num2; int square; cout<<"please enter a number"<<endl; cin>>num2; square = num2 *num2; cout<<" square of a number="<<square<<endl; } }
There's an extra } at the end also. And there are minor things that can be improved in your code (it will work without it, but if you work with real software you'll anyway need to follow good practices, so it's better to start getting used to them early 🙂) Formatting (it's better to follow rules of indentation for your code), in your case keep everything in main indented equally. It would increase readability of the code. At first you can try to use some tools for that https://codebeautify.org/cpp-formatter-beautifier It's better to declare variables as closer as possible to the first usage, the ideal (almost) case would be to do it in the same line int square = num1 * num1; using namespace std is completely fine for examples like this, but when you'll be writing real code it's better to use full types and values, e.g. std::cout. (there are exceptions, but for std stuff it's usually the case)
aAshu
How I started to learning c language??
Pavel
How I started to learning c language??
read a book, watch some online courses, try to code something
Anonymous
#include <iostream> using namespace std; void correction() { int a; cin >> a; if(cin) cout << "Input is valid!" << endl; else { cout << "Input is invalid... try " << "again!" << endl; cin.clear(); cin.ignore(100, '/n'); correction(); } } int main(int argc, char *argv[]) { correction(); }
Anonymous
Can anyone tell me why the recursion is not working well?
Anonymous
Please help me!
Anonymous
Yes, if invalid data passed to non matching data type