#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.
Related
function for get array from the user
#include <stdio.h>
void getArray()
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
}
function for display array
void displayArray(){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
getArray();
displayArray();
return 0;
}
The problem is how to pass the array that we get from the user to the display array function and both functions can be called in the main and also the array want to declare in the main function
An example that does not handle input errors.
In order for your functions to have knowledge of the array, you must send them its address as well as its size.
#include <stdio.h>
int getArray(int a[], int size_max)
{
int n;
printf("Enter size of array: ");
while(1)
{
scanf("%d",&n);
if(n>size_max) printf("The size must be less than %d: ", size_max);
else break;
}
printf("Enter %d elements in the array : ", n);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
return n;
}
void displayArray(int a[], int n)
{
printf("\nElements in array are: ");
for(int i=0; i<n; i++) printf("%d ", a[i]);
}
int main()
{
int a[1000];
int n = getArray(a, 1000);
displayArray(a, n);
return 0;
}
You can pass that shared variable as argument. Also return and use the returned value if reference not having valid data. Or else you need to pass this argument as reference instead of value like this.
#include <stdio.h>
int[] getArray(int a[])
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
return a;
}
function for display array
void displayArray(int a[]){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
a = getArray(a);
displayArray();
return 0;
}
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));
}
i want to copy element of first array to the second array with pointer
but after compiling i get message that is A problem caused the program to stop working correctly.windows will close the program and notify you if a solution is avilable
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[30],arr1[30];
int n,i;
int *p,*q;
p=arr;
q=arr1;
printf("Enter the no. of elements of array:-\n");
scanf("%d",&n);
printf("Enter the array element of 1st array:-\n");
for(i=0;i<n;i++)
{
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
q=(int*)*(p+i);
q++;
}
printf("elements of array 1=");
for(i=0;i<n;i++)
{
printf("%d , ",*(p+i));
}
printf("\nelements of array 2=");
for(i=0;i<n;i++)
{
printf("%d , ",*(q+i));
}
for(i=0;i<n;i++)
{
printf("%d , ",arr1[i]);
}
return 0;
}
Change
for(i=0;i<n;i++)
{
q=(int*)*(p+i);
q++;
}
To
for(i=0;i<n;i++)
{
q[i]=p[i];
}
As of now you are treating int as int * and assigning it to pointer leading to undefined behaviour.
Also *(p+i) is same as p[i] and (p+i) is same as &p[i]. But latter ones are more readable.
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.
the method which takes in pointer to pointer as argument
int findMax(int **a, int m, int n)
{
int max=**a,i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(max<=a[i][j]){
max=a[i][j];
}
}
}
return max;
}
This is the main function from where the findMax method is called.
int main()
{
// Variable Declaration
int m,n,i,j,a[50][50],*arr[50],**arrd;
// User Input
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
// Single Pointer Allocation
for(i=0;i<m;i++){
arr[i]=&a[i][0];
}
arrd=&arr[0];
// Output
printf("The matrix is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",findMax(arrd,m,n));
return 0;
}
I just want to find out max element in a 2d array using a function which takes in pointer to pointer of the array.
this code works fine but i am looking for a better approach...
#include <stdio.h>
#define NUMCOLUMNS 50
#define NUMROWS 50
int findMax(int (*a)[NUMCOLUMNS], int m, int n)
{
int max=**a,i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(max<=a[i][j]){
max=a[i][j];
}
}
}
return max;
}
int main()
{
// Variable Declaration
int m,n,i,j,a[NUMROWS][NUMCOLUMNS];
// User Input
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
// Output
printf("The matrix is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",findMax(a,m,n));
return 0;
}