Trying to insert element in an array using pointers, but not successfull - c

Help me out....There is no compile time error but there is some logical error which i am not able to sort out.
Input is taken from user without any problem but the elements are not getting inserted.
Output is unchanged array that user inputted.
void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t",*a+i);
}
getch();
}
void insert(int *b, int n)
{
if(n>=1)
{
printf("Insert Element at 2nd Position: ");
scanf("%d",b+1);
}
if(n>=4)
{
printf("Insert Element at 5th Position: ");
scanf("%d",b+4);
}
}

Your output is wrong, and it's caused by *a+i being interpreted as (*a)+i. This also shows up without any modification when you enter something other than a direct sequence:
Enter Size Of an array: 3
Enter Elements of an array: 1 2 9
gives the output:
1 2 3
which is clearly not right.
The solution, as mentioned in another couple of answers is to wrap your pointer-arithmetic with paranthesis: *(a+i).

"Output is unchanged array that user inputted."
Fix :-
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i)); //Notice `()`
}
FYI..this is not insertion, this is just an over-write !
For Insertion you could have something like following :
/*
b= original array
n= size of array (must be large enough)
pos = position of insertion
After call make sure to scan array till n+1
*/
void insert(int *b, int n, int pos)
{
int val,c;
printf("Enter the value to insert\n");
scanf("%d", &val);
for (c = n - 1; c >= pos - 1; c--)
b[c+1] = b[c];
b[pos-1] = val;
}

You should try using parenthesis around the a+i, e.g.
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t", /*SEE HERE */ *(a+i));
}
getch();
}

for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i)); //parenthesis added.
}
The parenthesis around the "(a+i)" are of utmost importance as together with the asterisk it indicates the value within that address. Errors like this are hard to find as they are very small but have a huge impact on the code.

Finally I solved the problem with the help of mistake pointed out by #P0W and #Mats and also applying concept of inserting element at any user defined position. Thanks Guys!!
So Here is the Corrected code. (And it is inserting element and not replacing it).
void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i));
}
getch();
}
void insert(int *b, int n)
{
int j;
if(n>=1)
{
printf("Insert Element at 2nd Position: ");
for(j=n-1;j>=1;j--)
{
b[j+1]=b[j];
}
scanf("%d",b+1);
}
if(n>=4)
{
printf("Insert Element at 5th Position: ");
for(j=n-1;j>=4;j--)
{
b[j+1]=b[j];
}
scanf("%d",b+4);
}
}

Related

Is there some thing wrong with the way i write my while loops

Why are my while loops not working? I m getting no error, but also no output.
Codes are working perfectly fine uptil the main logic.
example my output for binary search is :
Enter the array size:
5
Enter array elements in sorted form :
1 2 3 4 5
Enter the target element: 2
//no output is shown after this user input
(NOTE: Same logics are perrfectly working with for loops)
//Code for Binary search (while loop not working )
#include<stdio.h>
#define max 20
int main(){
int a[max],n,i,target;
int l=0;
int r=n-1;
int mid=(l+r)/2;
printf("\nEnter the array size: \n");
scanf("%d",&n);
printf("\nEnter array elements in sorted form : \n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the target element: ");
scanf("%d",&target);
while(l<=r){
if(target==a[mid]){
printf("Element found at %d position",i+1);
}
else if(target<a[mid]){
r=mid-1;
}
else{
l=mid+1;
}
}
return 0;
}
//Same problem with linear search while loop
#include<stdio.h>
#define max 20
int main(){
int array[max],target,size,i=0,flag=0;
printf("Enter the size of array: ");
scanf("%d",&size);
printf("Enter the elements of array: ");
for(i=0;i<size;i++){
scanf("%d",&array[i]);
}
printf("Enter the element to be found in array: ");
scanf("%d",&target);
printf("Target accepted");
while( i < size ) {
if(array[i]==target){
printf("Element found at %d position",i+1);
flag=1;
break;
}
else{
flag=0;
}
i++;
}
if(flag==0){
printf("Element not found !!!");
}
return 0;
}
In the while (l<=r) loop, either 'l' or 'r' value is changed, but mid is never changed, thus if(target==a[mid]) will always false.
You need to change 'mid' value in the while loop.
First you were initializing the right index before getting the size of the array so you will never enter the while loop. Second thing is that you should set the middle element inside your while loop so it can update properly every time you change the left and right variables.
check the code below:
int a[max],n,i,target;
int l=0;
printf("\nEnter the array size: \n");
scanf("%d",&n);
int r=n-1;
printf("\nEnter array elements in sorted form : \n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the target element: ");
scanf("%d",&target);
while(l<=r){
int mid=(l+r)/2;
if(target==a[mid]){
printf("Element found at %d position",mid);
break;
}
else if(target<a[mid]){
r=mid-1;
}
else{
l=mid+1;
}

Why is only the first element of the output wrong?

I have recently started practicing on C. The output is reversed except for first element. Can anyone tell me what is wrong here?
I also tried using for loop but that is displaying the array as it is.
#include<stdio.h>
#include<conio.h>
int main()
{
int temp;
int i,n;
printf("Enter value of n:");
scanf("%d",&n);
int array1[n];
int k=0,j=n;
printf("Enter the five numbers\n");
for(i=0;i<n;i++)
{
printf("Enter the number %d:",i+1);
scanf("%d",&array1[i]);
}
printf("The array is:\n");
for(i=0;i<n;i++)
{
printf("%d ",array1[i]);
}
while(k<j)
{
temp=array1[k];
array1[k]=array1[j];
array1[j]=temp;
j--;
k++;
}
printf("\nThe array after reversing is:\n");
for(i=0;i<n;i++)
{
printf("%d ",array1[i]);
}
}
You are assigning array1[k] to array1[j] and you initialized j to n, so it swaps the value in array1[k] with array1[j] => in the first iteration, the value j = n, however the index of the final element is n-1. (indexes are 0,1,2,3,4,...n-1). So it tries to get the value of position n, where there is actually no value stored ( it is outside the array ). Change j to n-1 in int k=0,j=n;

Output error 0 in a basic c program in finding repititions

I am trying to write a C program to print the number of repetitions for a given number in an array. what ever value I give as the input , the function returns the value only 0 to me.. what might be the error..the code is attached
#include<Stdio.h>
#include<conio.h>
int occurence(int n, int arr[], int x);
void main()
{
int x,arr[100],n,i;
clrscr();
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",arr[i]);
}
printf("\nEnter the element to be searched for repetitions: ");
scanf("%d",&x);
printf("%d",occurence(n,arr,x));
getch();
}
int occurence(int n,int arr[100], int x)
{
int i,rep=0;
for(i=0;i<n;i++)
{
if(x == arr[i])
{
rep++;
}
}
return rep;wenter code here
}
Problem is on the line 13 it is supposed to be scanf("%d",&arr[i]);

Cyclic Right Shift for C for an array- time exceeds-infinite loop?

I ran this code and got an time exceed error, It implements cyclic right shift to an array , given array size, elements, and shift width, some help on why this is causing an execution issue would be appreciated,i'm new to this :)
#include<stdio.h>
void main()
{
int n,i;
//array size input
scanf("%d",&n);
int a[n];
//array elements input
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
// shift amount input
int s,temp;
scanf("%d",&s);
//single right shift for S number of times
for(i=0;i<s;i++)
{
temp=a[n-1];
for(i=n-1;i>0;i--)
a[i]=a[i-1];
a[0]=temp;
}
//Output of shifted array
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
Main changes are required in that s nested loop as you used same variable in both outside and inside loops.
Corrections are marked as below:
live demo http://ideone.com/DaokVy
#include <stdio.h>
int main() // use properly
{
int n,i;
//array size input
scanf("%d",&n);
int a[100]; //edit
//array elements input
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
// shift amount input
int s,temp,j;
scanf("%d",&s);
for(i=0;i<s;i++)
{
temp=a[n-1];
for(j=n-1;j>0;j--) // use different variable here
a[j]=a[j-1];
a[0]=temp;
}
//Output of shifted array
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0; // exit success
}

The program wont work for n>9... It works fine when n is a static expression

if we take n>9 then it wont input the elements with arr[n] where n>9.
The program works fine if n is constant ..
whats wrong in program.
please help...
i complied and run it in windows using mingw shell
/* Binary Search Program */
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,arr[n],i,j,temp;
printf("Enter The number of elements in array : ");
scanf("%d",&n);
printf("Enter the %d elements of array :\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("The Unsorted array is :\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
/* BOUBBLE SORTING */
for(j=0;j<n-1;j++)
{
for(i=0;i<n-1-j;i++)
{
if(arr[i]>arr[i+1])
{
temp=arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
}
}
}
printf("The sorted array is :\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
int n,arr[n],i,j,temp;
The size of arr has an indeterminate value because n is not initialized.
Declare arr after your scanf("%d",&n) call.
You are using variable length arrays. On run time compiler did't know about n when you declare arr[n] before inputting n. You have to declare arr after inputting n.
Try this
int n,i,j,temp;
printf("Enter The number of elements in array : ");
scanf("%d",&n);
int arr[n];
Declare array arr under the scanf("%d",&n); at the beginning of new block or allocate memory with n * sizeof(int) bytes

Resources