Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab - 9: Pointers and structures, memory dereference.

(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];
};

void std_list(struct student*, char*, int);

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;
}

void std_list(struct student *ptr, char *str, int n)


{
int i;
printf("%-10s %-10s ", "HTNO.", "NAME");
printf("%-10s %-10s %-15s ", "MATHS", "PHYSICS", "CHEMISTRY");
printf("%-10s %-10s %-10s\n", "TOTAL", "AVERAGE", "GRADE");
for(i = 0; i < 90; i++)
printf("-");
for (i = 0; i < n; i++)
{
if (strcmp((ptr + i) -> grade, str) == 0)
{
printf("\n%-10s %-10s ", (ptr + i) -> htno, (ptr + i) -> name);
printf("%-10d %-10d ", (ptr + i) -> math, (ptr + i) -> phy);
printf("%-15d %-10.0f ", (ptr + i) -> che, (ptr + i) -> tot);
printf("%-10.2f %-10s ", (ptr + i) -> avg, (ptr + i) -> grade);
}
}
printf("\n");
for(i = 0; i < 90; i++)
printf("-");
printf("\n");
}
(iv) Aim: Read student name and marks from the command line and display the student
details along with the total.

Program:
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])


{
int i, total = 0;
char *str[] = {"File", "HTNo.", "Name", "Maths", "Physics", "Chemistry"};
printf("The student details are as follows: \n");
for(i = 1; i < argc; i++)
{
printf("%s: %s\n", str[i], argv[i]);
if (i > 2)
total += atoi(argv[i]);
}
printf("\nTotal = %d", total);
return 0;
}
(v) Aim: Write a C program to implement realloc().

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;
}

You might also like