**** OBVIOUSLY IN C LANGUAGE**** Structures We have modified the structures from
ID: 3824372 • Letter: #
Question
**** OBVIOUSLY IN C LANGUAGE****
Structures We have modified the structures from project 3 slightly. The part struct now has description being a pointer, and nextPart was added that will point to the next part in the kit. The kit structure now has a pointer firstPart that will point to the first part in the kit, and has a pointer nextKit that points to the next kit. The revised structs are: struct part 1 char description; int qtyPerkit; double costPerItem: struct part nextPart; struct kit f *kitName struct part first Part; double kitCost; struct kit nextKit;Explanation / Answer
Malloc space program :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Realloc pgm:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Address of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u ",ptr + i);
printf(" Enter new size of array: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2);
for(i = 0; i < n2; ++i)
printf("%u ", ptr + i);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.