Printing the given value using DMA - c

I'm coding to get the value of given number in Dynamic method using realloc. In this case the O/P of new size elements is not printing the correct value. Its printing some unknown garbage values after one or two elements.
This program to resize the integer value in the run time.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr,n,newsize;
printf("Enter size Elements\n");
scanf("%d", &n);
ptr = (int*) malloc (n * sizeof(int));
printf("Enter the Elements\n");
for(int i=0;i<n;i++)
{
scanf("%d", ptr+i);
}
printf("Elements are\n");
for(int i=0;i<n;i++)
{
printf("%u\n", *(ptr+i));
}
printf("Enter the New Size of the Elements\n");
scanf("%d", &newsize);
ptr = realloc(ptr,newsize);
printf("Enter the new Elements\n");
for(int i=0;i<newsize;i++)
{
scanf("%d", ptr+newsize);
}
printf("New Elements are\n");
for(int i=0;i<newsize;i++)
{
printf("%u\n", *(ptr+i));
}
return 0;
}
Expecting:
1
2
3
4
Actual Results:
1
2
4566745
4534665

Its working now after making below correction said by Johnny Mopp.
printf("New Elements are\n");
for(int i=0;i<newsize;i++)
{
printf("%d\n", *(ptr+i));
}

Related

I am making a Menu driven program in which i have to create, delete and insert an array

I am making a Menu driven program in which I have to create, delete and insert an array but when I am calling the delete or insert function because of that my code is not running please help. please tell me how to call the function so the code can run. if I remove of comment the calling of indDelete and IndInsert my code is running perfectly but as I uncomment them the code does not run.
#include <stdio.h>
#include <stdbool.h>
//printing the array
void display(int arr[], int size){
for(int i = 0; i < size; i++){
printf("%d ",arr[i]);
}
printf(" /n");
}
//insertion function
int indInsert(int arr[], int capacity, int size, int element, int index){
if(size>=capacity){
return -1;
}
for(int i = size-1; i >= index; i--){
arr[i+1] = arr[i];
}
arr[index] = element;
return 1;
}
//deletion function
int indDelete(int arr[], int capacity, int size, int index){
if(index > capacity || index < 0){
return -1;
}
for(int i = index; i < size-1; i++){
arr[i] = arr[i+1];
}
return 1;
}
//driver code
int main(){
int capacity, arr[capacity], size;
printf("Enter the capacity of an array \n");
scanf("%d", &capacity);
printf("Enter the number of elements you want to enter \n");
scanf("%d", &size);
printf("Enter the elements:- \n");
for(int i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
bool check = false;
int exit = 0, element = 0, index = 0;;
while(check == false){
printf("If you want to insert an element press 1 \n");
printf("If you want to delete an element press 2 \n");
printf("If you want to exit press 3 \n");
scanf("%d", &exit);
switch(exit){
case 1:
printf("Enter the element:- \n");
scanf("%d", &element);
printf("Enter the index:- \n");
scanf("%d", &index);
printf("The present Array \n");
display(arr, size);
printf("The insterted array \n");
indInsert(arr,capacity,size,element,index);
size +=1;
display(arr, size);
break;
case 2:
printf("Enter the index:- \n");
scanf("%d", &index);
printf("The present Array \n");
display(arr, size);
printf("The insterted array \n");
indDelete(arr,capacity,size,index);
size -= 1;
display(arr, size);
break;
case 3:
printf("Thank you for using the program");
check = 111;
break;
default:
printf("Error: Wrong cmd");
break;
}
}
return 0;
}
On the first line of main you have
int capacity, arr[capacity], size;
Which means arr is an array of unknown size, because capacity isn't defined yet. Even if you change capacity later, the array size won't change.
If you turn on your compiler warnings you will probably be notified of this, like with -Wall for gcc or /Wall for msvc.

Copy all array elements into another array in C

I done with my logic which is actually used to copy a Array elements into another Array but in the final output(Point 1) of printing statement is not working well as I expecting.
#include <stdio.h>
int main()
{
int arr[50],n,key,loc;
printf("Enter size of the Elements:\n");
scanf("%d", &n);
printf("Enter %d Elements\n", n);
for(int i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
//int a = arr[i];
printf("Enter the Element to insert:\n");
scanf("%d", &key);
printf("Enter the Location to insert:\n");
scanf("%d", &loc);
for(int i=n-1;i>=loc;i--)
{
arr[i+1] = arr[i];
}
arr[loc] = key;
printf("Result of Array:\n");
for(int i=0;i<=n;i++) //Point 1
{
printf("%d\n", arr[i]);
}
return 0;
}
Its expecting to print the copied value to print but its not showing the last element of the array.
eg:
a[] = 1,2,3
b[] = 8,9
Expecting o/p:
1,2,3,8,9
Actual o/p:
1,2,3,8
When you insert your element into your array, the size of your array increases; you should put a n++ before printing.
Are you sure that is the right code that you are trying to solve? I don't see anything wrong with that logic except the potential for buffer overflow from the input. I think your logic is correct. But instead of declaring a size for the array, you can just let the value from user input do that. I posted the code below, just a small fix to make your code to become dynamic because there isn't anything wrong with your code logic.
#include <stdio.h>
int main()
{
int n,key,loc;
printf("Enter size of the Elements:\n");
scanf("%d", &n);
int arr[n + 1];
printf("Enter %d Elements\n", n);
for(int i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
//int a = arr[i];
printf("Enter the Element to insert:\n");
scanf("%d", &key);
printf("Enter the Location to insert:\n");
scanf("%d", &loc);
for(int i=n-1;i>=loc;i--)
{
arr[i+1] = arr[i];
}
arr[loc] = key;
printf("Result of Array:\n");
for(int i=0;i<=n;i++) //Point 1
{
printf("%d\n", arr[i]);
}
return 0;
}

program to print the position of the smallest number of n numbers using arrays

This code does not give me answer when i enter value 1 in array.
for eg i have taken no. of elements as 5
then i enter them as 2,3,1,6,4
then output gives 2 as smallest number and position number is not always correct
what's the error?
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
small=a[0];
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
"small" variable is overridden with a[0] in each iteration. Just move it outside the loop:
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
You are not that far away - use are initializing small=a[0]; every time whenever loop iterates so just initialize small=a[0]; before loop
Here your corrected code
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
small = a[0] should be before the loop.
You should write small = a[0] before the beginning of a for loop.

C Program, User input into a single array

I'm trying to use three integers that are inputted by a user in a single line into an array using CodeBlocks. But i don't really know how to go about this. Can anyone help me?
Thanks!
main()
{
int arr[3];
int onenum;
int twonum;
int threenum;
printf("Enter an Input: ");
scanf("%d %d %d",&onenum,&twonum,&threenum);
printf("Your input is: %d %d %d \n",onenum,twonum,threenum);
int arr [onenum, twonum, threenum];
return 0;
}
Use this
int i;
int arr[3];
printf("Enter numbers");
for(i=0;i<3;i++){
scanf("%d",&arr[i]);
}
This will store the 3 numbers entered by user in array arr.
Like this:
void main()
{
int arr[3];
int i;
printf("Enter an Input: ");
for(i = 0; i < 3; i++)
{
scanf("%d", &arr[i]);
}
printf("Your input is: ");
for(i = 0; i < 3; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Let's use a loop from which runs from i=0 to i=2 because you have to add 3 numbers in array.
void main()
{
int arry[3],i=0;
printf("enter numbers");
for(i=0;i<3;i++)
{
scanf("%d",&arry[i]);
}
for(i=0;i<3;i++)
{
printf("%d \n",arry[i]);
}
}
Array is a collection of data.
For beginning you can think a array with different index as a different variable of same data type means arry[0] and arry[1] in this example as different variables of same data type integer.It will help you for beginning with array but keep in mind that arry is the only one variable the index inside capital bracket tells the variable where to look.

2-D matrix sum using pointer to integers

#include<stdio.h>
int main()
{
int (*p)[3],i,j;
int (*q)[3];
int (*r)[3];
printf("Enter 6 integers of first matrix:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",*(p+i)+j);
printf("The matrix you have entered is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ", *(*(p+i)+j));
}
printf("\n");
}
printf("Enter 6 integers of second matrix:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",*(q+i)+j);
printf("The matrix you have entered is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ", *(*(q+i)+j));
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
*(*(r+i)+j)=*(*(p+i)+j) + *(*(q+i)+j);
}
}
printf("The summation matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ", *(*(r+i)+j));
}
printf("\n");
}
}
In this program I have declared 3 pointers to an array of 3 integers. when I execute, the first matrix works fine and displayed. However, when I enter integers for the second matrix it crashes. I tried a lot but it fails.
As you say you have a pointer , pointer should point to some memory location before writing something to it.
In your case the pointer doesn't point to any valid memory location and hence the crash.
If you want a 2*3 matrix then you should be having
char *p[2];
p[0] = malloc(sizeof(int) *3);
Then
p[i][j] is valid.
Similarly you should allocate memory for each pointers individually.

Resources