Professional Documents
Culture Documents
CP Lab 9
CP Lab 9
(i) Aim: Write a C program to find the sum of a 1D array using malloc()
Program:
#include<stdio.h>
#include<malloc.h>
int main()
{
int *ptr, sum = 0;
int i, n;
printf("Enter the size of 1D integer array: ");
scanf("%d", &n);
ptr = (int *) malloc (n * sizeof(int));
if (ptr == NULL)
printf("\nMemory is not allocated.\n");
else
{
printf("\nMemory is successfully allocated using malloc().\n");
printf("\nEnter %d values one by one: \n", n);
for (i = 0; i < n; i++)
scanf("%d", (ptr + i));
printf("\nThe given 1D integer array is as follows: \n");
for (i = 0; i < n; i++)
{
printf("%d\t", *(ptr + i));
sum += *(ptr + i);
}
printf("\n\nThe sum of values of given 1D integer array = %d", sum);
}
return 0;
}
(ii) Aim: Write a C program to find the total, average of n students using structures.
Program:
#include<stdio.h>
struct student
{
char htno[11];
char name[11];
int math, phy, che;
float tot, avg;
};
int main()
{
struct student std[10];
int i, n;
printf("Enter number of students (Max. 10): ");
scanf("%d", &n);
printf("\nEnter the details of %d student(s) one by one: \n", n);
for (i = 0; i < n; i++)
{
printf("\nEnter student %d details: ", (i + 1));
printf("\nHall Ticket No.: ");
scanf("%s", std[i].htno);
printf("Name: ");
scanf("%s", std[i].name);
printf("Enter subjects\' marks:\n");
printf("Maths: ");
scanf("%d", &std[i].math);
printf("Physics: ");
scanf("%d", &std[i].phy);
printf("Chemistry: ");
scanf("%d", &std[i].che);
std[i].tot = std[i].math + std[i].phy + std[i].che;
std[i].avg = std[i].tot / 3;
}
printf("\nThe students\' report is as follows: \n\n");
printf("%-12s %-12s", "HTNO.", "NAME");
printf("%-12s %-12s %-12s", "MATHS", "PHYSICS", "CHEMISTRY");
printf("%-12s %-12s\n", "TOTAL", "AVERAGE");
for(i = 0; i < 85; i++)
printf("-");
for (i = 0; i < n; i++)
{
printf("\n%-12s %-12s", std[i].htno, std[i].name);
printf("%-12d %-12d %-12d", std[i].math, std[i].phy, std[i].che);
printf("%-12.0f %-12.2f", std[i].tot, std[i].avg);
}
return 0;
}
(iii) Aim: Enter n students data using calloc() and display failed students list.
Program:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct student
{
char htno[11];
char name[11];
int math, phy, che;
float tot, avg;
char grade[5];
};
int main()
{
struct student *ptr;
int i, n;
printf("Enter number of students: ");
scanf("%d", &n);
ptr = (struct student *) calloc (n, sizeof(struct student));
if (ptr == NULL)
{
printf("Memory is not allocated.");
goto stop;
}
printf("\nEnter the details of %d students one by one: \n", n);
for (i = 0; i < n; i++)
{
printf("\nEnter student %d details: ", (i + 1));
printf("\nHall Ticket No.: ");
scanf("%s", (ptr + i) -> htno);
printf("Name: ");
scanf("%s", (ptr + i) -> name);
printf("Enter subjects\' marks:\n");
printf("Maths: ");
scanf("%d", &(ptr + i) -> math);
printf("Physics: ");
scanf("%d", &(ptr + i) -> phy);
printf("Chemistry: ");
scanf("%d", &(ptr + i) -> che);
(ptr + i) -> tot = (ptr + i) -> math + (ptr + i) -> phy + (ptr + i) -> che;
(ptr + i) -> avg = (ptr + i) -> tot / 3;
if ((ptr + i) -> avg >= 40)
strcpy((ptr + i) -> grade, "PASS");
else
strcpy((ptr + i) -> grade, "FAIL");
}
printf("\nThe passed students\' list is as follows: \n\n");
std_list(ptr, "PASS", n);
printf("\nThe failed students\' list is as follows: \n\n");
std_list(ptr, "FAIL", n);
stop:
return 0;
}
Program:
#include<stdio.h>
#include<stdlib.h>
Program:
#include<stdio.h>
#include<malloc.h>
int main()
{
char *str;
str = (char *) malloc (31 * sizeof(char));
if (str == NULL)
{
printf("Memory is not allocated.");
goto halt;
}
printf("Enter your short name (Max. 30 characters): \n");
gets(str);
printf("Your short name is: %s \n", str);
str = (char *) realloc (str, 101 * sizeof(char));
if (str == NULL)
{
printf("Memory is not allocated.");
goto halt;
}
printf("Enter your full name (Max: 100 characters): \n");
gets(str);
printf("Your full name is: %s \n", str);
halt:
return 0;
}