Question & Answer: This is a memory allcation C question:…..

This is a memory allcation C question:

I need to create a 2D array that holds different versions of the same struct – each array[][] will hold different x, y values. The layout is found below.

How would I implement this? How would I update data in the program in array[1][0] after it is implemented?

Thanks

/01 3) iO 20 Struct Data nt x int

/01 3) iO 20 Struct Data nt x int

Expert Answer

 

Answer:

#include<iostream>

#include<stdio.h>

using namespace std;

struct data

{

int x,y;

};

int main(){

int reacod;

struct data*ptr;

printf(“Enter the recodsn”);

scanf(“%s %d”,&reacod);

ptr = malloc ( sizeof(struct data));

for(int i=0;i<reacod;i++)

{

for(int j=0;i<reacod;j++)

{

printf(“Enter the values of x and y”);

scanf(“%s %d”, &(ptr+i)->x,&(ptr+j)->y);

if(ptr[i]==1&&ptr[j]==0)

{

printf(“Enter the values to be updated”);

scanf(“%s %d”, &(ptr+i)->x,&(ptr+j)->y);

}

}

}

}

Still stressed from student homework?
Get quality assistance from academic writers!