home / homework help / questions and answers / engineering / computer science /
ID: 662665 • Letter: H
Question
home / homework help / questions and answers / engineering / computer science / write a function indexarray(n) that returns a pointer ...
Your question has been answered! Rate it below.
Let us know if you got a helpful answer.
Question
Write a c program. Write a function IndexArray(n) that returns a pointer to a dynamically allocated integer array with n elements, each of which is initialized to its own index. For example, assuming that ip is declared as int *ip; the statement ip = IndexArray(10); should produce the following memory configuration:
Format shouuld be
Please enter the size of the array: 5
The resulting array is:
0, 1, 2, 3, 4
when a charcter is entered
Please enter the size of the array: a
Please enter an integer
Retry:
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main() {
int i, n, *ip;
printf("Please enter the size of an array : ");
scanf("%d",&n);
ip=(int*)calloc(n,sizeof(int));
if(ip==NULL)
{
printf("Memory not allocated");
exit(0);
}
for(i=0;i<n;++i)
{
scanf("%d",ip+i);
for(i=0;i<n;++i)
printf("The resulting array is: %d ",*(ip+i));
}
return(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.