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;
Related
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;
}
WAP to sort an array in ascending order.
Issue: After Sorting the array getting the garbage value in the compiler. Also when done without a function compiler return nothing
#include<stdio.h>
//Global Decleration
int i, j, size;
//Function for bubble sort
int bubblesort(int arr[size]){
int temp;
printf("The numbers arranged in ascending order are given below: \n");
for(i=0;i<size-1;i++){
for(j=0;j<size-1-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(j=0;j<size;j++)
printf("%d\n",arr[j]);
}
int main(){
int size, arr[size];
printf("Enter the number of elements of the array: \n");
scanf("%d", &size);
printf("Enter the numbers: \n");
for(i=0;i<size;i++){
scanf("%d", &arr[i]);
}
bubblesort(arr[size]);
}
I am trying to get user input and store them in an array Fib[i]. After that print the Fib[i] array. When the user enters -1 the loop will quit and the program will end. But my code is not printing or terminating.
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i;
for(i=0; i<50; i++)
{
scanf("%d", &Fib[i]);
if(i==-1)
break;
//printf("numbers entered %d\n", Fib[i]); // <- doesn't terminate if printf is here
}
printf("numbers entered %d\n", Fib[i]); //doesn't print anything??
}
int main()
{
int i, n;
//calling the function
fib(n);
return 0;
}
user input:
4
5
-1
Expected output:
Numbers entered
4
5
First issue: you declare Fib as an array of double:
double Fib[50];
But you use %d to read the values, which is for reading an int:
scanf("%d", &Fib[i]);
Using the wrong format specifier invokes undefined behavior. You presumably want to store integers, so change the array to int:
int Fib[50];
Next is your array breakout condition:
if(i==-1)
i is your array index, which ranges from 0 to 49, so this will never be true. You want to stop when the user enters -1, and that value will be in Fib[i]:
if(Fib[i]==-1)
Finally, printing the array:
printf("numbers entered %d\n", Fib[i]);
This doesn't print the array. It just prints the element at the last index of i, and the value at that index will always be -1. You need a separate loop to print the values:
int j;
printf("numbers entered:\n");
for (j=0; j<i; j++) {
printf("%d\n", Fib[j]);
}
This code has many code writing standard issues but it seems you are new so I am making minimal changes just for your understanding
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i,j;
for(i=0; i<50; i++)
{
scanf("%lf", &Fib[i]);
if(Fib[i]==-1)
break;
}
printf("numbers entered \n");
for(int j=0;j<i;j++)
{
printf("%lf\n",Fib[j]);
}
}
int main()
{
int i, n;
fib(n);
return 0;
}
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
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);
}
}