Function insertionSort does not sort the array [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is my code. After the call to the function insertionSort, when i print the array it prints the array without sorting it.
I am unable to understand if the problem is in the sorting algorithm or some other problem. The code compiles just fine and runs too, so that excludes the probability of any syntactic errors.
#include<stdio.h>
void insertionSort(int arr[], int n);
int main(){
int n, i;
printf("Enter n: ");
scanf("%d\n", &n);
int arr[n];
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
insertionSort(arr, n);
for(i=0; i<n; i++){
printf("%d\n", arr[i]);
}
}
void insertionSort(int arr[], int n){
int i,j, key;
for(j=1; j<n; j++){
key = arr[j];
i = j-1;
while (i>0 && arr[i] > key) {
arr[i+1] = arr[i];
arr[i] = key;
i--;
}
}
}

You forgot to include arr[0] to the candidate to be inserted.
Try using while (i>=0 && arr[i] > key) instead of while (i>0 && arr[i] > key) for the loop.

Related

Reason and solution of my code which is giving segmentation fault in C program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
#include <stdio.h>
int display(int arr[], int n);
int main()
{
int i, n;
printf("\nEnter the Size of Array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the %d Values of Arrays", n);
for (i = 0; i < n; i++)
{
scanf("%d", arr[i]);
}
display(&arr[0], n);
return 0;
}
int display(int arr[], int n)
{
int i;
printf("\nThe %d elements are:\n");
for (i = 0; i < n; i++)
{
printf("Array [%d]= %d\n", i, arr[i]);
}
}
This is the code for printing the array values using function. And in Run-time it gives Segmentation fault error. Help me to fix this.
You have to pass the address of the variable when calling scanf() in the loop, just as you do when reading n.
When passing an array to a function, we usually just write the name of the array. This is equivalent to passing &arr[0], but we don't normally write that out.
#include <stdio.h>
int display(int arr[], int n);
int main()
{
int i, n;
printf("\nEnter the Size of Array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the %d Values of Arrays", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
display(arr, n);
return 0;
}
int display(int arr[], int n)
{
int i;
printf("\nThe %d elements are:\n");
for (i = 0; i < n; i++)
{
printf("Array [%d]= %d\n", i, arr[i]);
}
}

Why this code which is posted on howstuffworks.com does not work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include <stdio.h>
int main()
{
int a[5];
int i;
for (i=0; i<5; i++)
a = i;
for (i=0; i<5; i++)
printf("a[%d] = %d\n", i, a);
}
Arrays used as operand of operatiors except for sizeof and unary & are automatically converted to a pointer that points at the first element of the array.
The converted pointer is not a lvalue, so it cannot be used as the left operand of assignment operator.
This code works.
#include <stdio.h>
int main(void)
{
int a[5];
int i;
for (i=0; i<5; i++)
a[i] = i;
for (i=0; i<5; i++)
printf("a[%d] = %d\n", i, a[i]);
return 0;
}
You declare a to be an array of integers - and instead of accessing an element of the array a[i] you access a directly, which only holds the memory adress of the first element of the array. So you are basically modifying memory adresses directly, which is almost never a good idea.
yu have error in a =i, should a[i]=i:
int main()
{
int a[5];
int i;
for (i=0; i<5; i++)
a[i] = i;
for (i=0; i<5; i++)
printf("a[%d] = %d\n", i, a[i]);
}

Can somebody please help me with the mistake in algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Problem Link: https://www.codechef.com/problems/PERMUT2
Problem : Getting non ambiguous for all test cases. There is absolutely no problem in executing the program, no errors.
Can you please point out the mistake in my code/algorithm:
#include <stdio.h>
#include <stdlib.h>
int index_func(int number, int *array, int x);
int main(){
int n;
scanf("%d", &n);
int *nums = (int*)malloc(n*sizeof(int));
int i;
for(i=0; i<n; i++){
scanf("%d", &nums[i]);
}
int j;
int counter = 0;
for(j=0; j<n; j++){
if(nums[j] != index_func(j+1, nums, n)){
counter = 1;
break;
}
}
if(counter == 0){
printf("ambiguous\n");
}else{
printf("non ambiguous\n");
}
return 0;
}
int index_func(int number, int *array, int x){
int z, index;
for(z=0; z<x; z++){
if(number == array[z]){
index = z;
return z;
}
}
}
The numbers in the array start with one, but the indices in C arrays start with 0. A quick fix to your program would be to add one to the returned index when you compare it to the current number:
if (nums[j] != index_func(j + 1, nums, n) + 1) ...
An alternative solution is to adjust the array data by subtracting one after you scan it, so that the array contains zero-based numbers.
A problem may arise with larger arrays, because every call to index_func scans the whole array from the beginning and will traverse half of it on average. The solution will be correct, but very slow.
But you don't have to determine the index to do the comparison. It is sufficient to check whether the number at the index of the current number is the current index. That leads to this function:
int is_ambiguous(const int *array, int n)
{
int i;
for (i = 0; i < n; i++) {
if (array[array[i] - 1] != i + 1) return 0;
}
return 1;
}
Some notes on your original code:
You should return an invalid index, probably −1, from index_funct when the nuber isn't in the array. I know, this shouldn't happen here, but next time you copy and paste the code and the missing return value might bite you.
You don't really need the variable index in index_funct. Separating pieces of code into small functions can make the program control easier. Compare the above function is_ambiguous with your inline solution with a counter variable and a break.
When you allocate, you must also free, which you don't.
try this solution:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int index_func(int number, int *array, int x);
int main(){
int n;
scanf("%d", &n);
int *nums = (int*)malloc(n*sizeof(int));
int i;
for(i=0; i<n; i++) {
scanf("%d", &nums[i]);
}
int j;
int counter = 0;
for(j=0; j<n-1; j++){
if((abs(nums[j+1] - nums[j]) != abs(n-1)) && (abs((nums[j+1] - nums[j]) != 1)))
{
counter = 0;
}
else
{
counter = 1;
}
}
if(counter == 0){
printf("ambiguous\n");
}else{
printf("non ambiguous\n");
}
free(nums);
return 0;
}

I am getting the error "expected declaration specifiers or ‘...’ before ‘(’ token"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
int i, F(int);
int F((int - 1) + (int - 2))
{
return (int - 1) + (int - 2)
}
for(i=1; i<=15; i++)
{
F(1) == 0;
F(i) == F(i-1)+F(i-2);
printf("F(%d) = %d", i, F(i));
}
return 0;
}
I am trying to print the fibonacci sequence from the first to the fifteenth number
#include <stdio.h>
int F(int n){
if(n==1)
return 0;
if(n==2)
return 1;
return F(n - 1) + F(n - 2);
}
int main(){
int i;
for(i=1; i<=15; i++){
printf("F(%d) = %d\n", i, F(i));
}
return 0;
}

Copying of array leads to 0's? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am learning C and one of my functions (not shown here) depends on two arrays being the same. I have one array and am trying to generate a copy but what happens is that both arrays turn out to be full of 0's after the copy. I have no idea how this happens.. Can anybody help explain why this happens and offer a solution on how to do it right?
#include <stdio.h>
int main(){
int array[5]={3,2,7,4,8};
int other_array[5]={0,0,0,0,0};
for (int i=0; i<5; i++) array[i]=other_array[i]; //Copies array into other_array
printf("array is:\n");
for (int j=0; j<5; j++) printf("%d", array[j]);
printf("\nother_array is:\n");
for (int i=0; i<5; i++) printf("%d", other_array[i]);
printf("\n");
return 0;
}
/*
When run this yields
**********************
array is:
00000
other_array is:
00000
*/
//Copies array into other_array
for (int i=0; i<5; i++) array[i]=other_array[i];
Try:
other_array[i] = array[i];
The assignment operator assigns the value of the right operand into the object in the left operand. And not the opposite.
Also, as said in other answers:
printf("\nother_array is:\n");
for (int i=0; i<5; i++) printf("%d", array[i]);
Your are printing array and not other_array.
You're printing the wrong array, you print array twice
printf("array is:\n");
for (int j=0; j<5; j++) printf("%d", array[j]);
printf("\nother_array is:\n");
for (int i=0; i<5; i++) printf("%d", other_array[i]);//<-- here
printf("\n");
Of course you could just write:
int main(){
int array[] = { 3, 2, 7, 4, 8};
int other_array[] = { 0, 0, 0, 0, 0};
memcpy(other_array, array, sizeof other_array);
/* then print */
}
It might also be a good idea to #include <assert.h> and then:
assert(sizeof array == sizeof other_array)

Resources