so here is the question : Aside from the array declaration in item 1, use pointe
ID: 3819459 • Letter: S
Question
so here is the question :
Aside from the array declaration in item 1, use pointer notation for everything else
Declare a two dimensional array (3 x 2) of integers
Declare a pointer variable and point it to the two dimensional array
Populate the contents of the first two rows
Call a function that has two parameters: the entire array, the last row of the array
Inside the function, use a for loop to come up with the cumulative total for each COLUMN and place the result in the third row.
Return back only the third row of the array
Inside main, traverse through the contents of the third row that was passed back and display the cumulative results for each column
and I was working on it and its what i got so far
#include<iostream>
#include<cstdio>
using namespace std;
func(array);
void func(array,array[])
int main(){
int array[2][3];
int (*p)[3]=array;
sum=0;
//populate the array
for(int i=0; i<2;i++){
for(int j=0;j<2;j++){
printf("Enter the number for column %d,and row %d:",i+1,j+1);
scanf("%d",&array[i][j]);
}
}
//function
for(p=&array[0][0];p<=&array[2][2];p++){
sum+=*p
cout<<endl<<sum;
}
}
Thank you
Explanation / Answer
#include<stdio.h>
//void func(int (*arr)[3][2],int (*array)[2])
void func(int (*arr)[3][2],int (*array)[2])
{
int i,j;
int *ptr=(int*)array;
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",(*arr)[i][j]);
}
printf(" ");
}
for(i=0;i<2;i++)
{
*(ptr+i)=0;
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
ptr[j]+=(*arr)[i][j];
}
printf(" Third row: ");
for(i=0;i<2;i++)
{
printf("%d ",ptr[i]);
}
}
int main()
{
int array[3][2]; // Array of size 3*2
int (*pToArr)[3][2]=&array; //Pointer to a 2D array of size int[3][2]
int (*p)[2]=&array[3]; // Pointer to an array of 2 integers
int i,j;
//populate the first 2 rows
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter the number for row %d and column %d:",i+1,j+1);
scanf("%d",&(*pToArr)[i][j]);
}
}
pToArr=&array;
func(pToArr,p);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.