In: Computer Science
Q1: Constraint:
Use concept of dynamic allocation for implementation
Statement:
In a class there are N students. All of them have appeared for the
test. The teacher evaluated
the test and noted marks according to their roll numbers. Marks of
each students has to be incremented
by 5. Print list of marks of students before and after
increment.
Dynamic memory allocation is a feature provided during the run time of the program. In dynamic memory allocation the data are stored in the heap memory. In dynamic memory allocation for allocating a variable value we are using pointers. For this dynamic memory allocation there are some functions. These functions are:
Since in the above question the language is not specific. I have used C language for the compliation of the code. I am attaching the code along with the screenshot of both input and output.
PROGRAM CODE :
#include <stdio.h>
#include<stdlib.h>
int main()
{
int *arr;
int N,i;
int *new_array;
printf(" Enter total number of students details wanted to be input :");
scanf("%d",&N);
/* allocation of memory dynamically */
arr = (int*)malloc(N*sizeof(int));
if(arr==NULL)
{
printf(" Insufficient Memory...\n");
return 0;
}
printf(" Enter %d the marks of the students : \n ",N);
for(i=0;i<N;i++)
{
scanf("%d",(arr+i));
}
printf("\n");
printf("MARK OF STUDENTS BEFORE INCREMENTING\n");
printf("------------------------------------\n");
for(i=0;i<N;i++)
{
printf("The student of Roll Number %d has %d marks \n",i+1,(*(arr+i)));
}
printf("\n");
printf("\n");
printf("MARK OF STUDENTS AFTER INCREMENTING\n");
printf("-----------------------------------\n");
for(i=0;i<N;i++)
{
printf("The student of Roll Number %d has %d marks \n",i+1,(*(arr+i)+5));
}
return 0;
}
SCREENSHOT OF INPUT
SCREENSHOT OF OUTPUT