0% found this document useful (0 votes)
37 views9 pages

CP Week9

The document contains the details of 5 programming assignments related to pointers, structures and dynamic memory allocation in C language: 1) Write a program to find the sum of elements in an integer array dynamically allocated using malloc. 2) Write a program to find total and average marks of students using a structure to store student details. 3) Write a program to enter student data using calloc and display the list of failed students. 4) Write a program to read student names and marks from command line and display details along with total marks. 5) Write a program to implement reallocation of memory to an array using realloc.

Uploaded by

MUKESH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

CP Week9

The document contains the details of 5 programming assignments related to pointers, structures and dynamic memory allocation in C language: 1) Write a program to find the sum of elements in an integer array dynamically allocated using malloc. 2) Write a program to find total and average marks of students using a structure to store student details. 3) Write a program to enter student data using calloc and display the list of failed students. 4) Write a program to read student names and marks from command line and display details along with total marks. 5) Write a program to implement reallocation of memory to an array using realloc.

Uploaded by

MUKESH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

WEEK 9

Lab 9: Pointers and structures, memory dereference.

i) Write a C program to find the sum of a ID array using malloc( )


Aim: To write a C program to find the sum of a ID array using malloc( )
Algorithm:
Step 1: Input the size of the array (n) from the user.
Step 2: Dynamically allocate memory for an integer array of size n using malloc( ).
Step 3:Check if memory allocation is successful. If not, display an error message and exit
the program.
Step 4:Input the elements of the array from the user.
Step 5:Initialize a variable (sum) to zero to store the sum of the array elements.
Step 6:Calculate the sum of the array elements using a loop.
Step7:Display the sum of the array elements.
Step8:Free the dynamically allocated memory using free( ).
Step9:Exit the program.

Program:
#include <stdio.h>
#include <stdlib.h>
int main( )
{
int i,n; // Size of the array
int *arr; // Pointer to the array
int sum = 0; // Variable to store the sum
// Step 1: Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Step 2: Dynamically allocate memory for the array
arr = (int *)malloc(n * sizeof(int));
// Step 3: Check if memory allocation is successful
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit the program with an error code
}
// Step 4: Input elements of the array from the user
printf("Enter %d elements:\n", n);
for ( i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Step 5: Calculate the sum of the array elements
for ( i = 0; i < n; i++)
{
sum += arr[i];
}
// Step 6: Display the sum
printf("Sum of the array elements: %d\n", sum);
// Step 7: Free the dynamically allocated memory
free(arr);
// Step 8: Exit the program successfully
return 0;
}

Result:
Enter the size of the array: 5
Enter 5 elements:
10
20
30
40
50
Sum of the array elements: 150

ii) Write a C program to find the total, average of n students using structures
Aim: To write a C program to find the total, average of n students using structures
Algorithm:
Step1: Input the number of students (n) from the user.
Step 2: Declare a structure to represent a student with fields for name and marks.
Step 3: Declare an array of structures to store information about each student.
Step 4: For each student, input their name and marks from the user.
Step 5: Calculate the total marks by summing up the marks of all students.
Step 6: Calculate the average marks by dividing the total marks by the number of students (n).
Step7: Display the total and average marks.
Step 8: Exit the program.
Program:
#include <stdio.h>
// Define a structure to represent a student
struct Student
{
char name[50];
int marks;
};

int main( )
{
Int i, n;
// Step 1: Input the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Step 2: Declare a structure to represent a student
// Step 3: Declare an array of structures to store information about each student
struct Student students[n];
// Step 4: Input details for each student
for ( i = 0; i < n; i++)
{
printf("Enter the name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter the marks of student %d: ", i + 1);
scanf("%d", &students[i].marks);
}
// Step 5: Calculate total marks
int total = 0;
for ( i = 0; i < n; i++)
{
total + = students[i].marks;
}
// Step 6: Calculate average marks
float average = (float)total / n;
// Step 7: Display total and average marks
printf("Total marks: %d\n", total);
printf("Average marks: %.2f\n", average);
// Step 8: Exit the program
return 0;
}
Result:
Enter the number of students: 3
Enter the name of student 1: Alice
Enter the marks of student 1: 85
Enter the name of student 2: Bob
Enter the marks of student 2: 90
Enter the name of student 3: Charlie
Enter the marks of student 3: 78

Total marks: 253


Average marks: 84.33
iii)Enter n students data using calloc( ) and display failed students list
Aim: To write a C program to Enter n students data using calloc( ) and display failed students
list.
Algorithm:
Step1: Input the number of students (n) from the user.
Step 2: Dynamically allocate memory for an array of structures using calloc().
Step3: Check if memory allocation is successful. If not, display an error message and exit the
program.
Step 4: For each student, input their name and marks from the user.
Step 5: Display the list of failed students (marks less than 40).
Step 6: Free the dynamically allocated memory using free().
Step 7: Exit the program.
Program:
#include <stdio.h>
#include <stdlib.h>
// Define a structure to represent a student
struct Student
{
char name[50];
int marks;
};
int main( )
{
int i, n;
// Step 1: Input the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Step 2: Dynamically allocate memory for an array of structures
// Step 3: Check if memory allocation is successful
struct Student *students = (struct Student *)calloc(n, sizeof(struct Student));
if (students == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit the program with an error code
}
// Step 4: Input details for each student
for (i = 0; i < n; i++)
{
printf("Enter the name of student %d: ", i + 1);
scanf("%s", students[i].name);

printf("Enter the marks of student %d: ", i + 1);


scanf("%d", &students[i].marks);
}
// Step 5: Display the list of failed students (marks less than 40)
printf("\nList of failed students:\n");
for ( i = 0; i < n; i++)
{
if (students[i].marks < 40)
{
printf("Name: %s, Marks: %d\n", students[i].name, students[i].marks);
}
}
// Step 6: Free the dynamically allocated memory
free(students);
// Step 7: Exit the program successfully
return 0;
}
Result:
Enter the number of students: 3
Enter the name of student 1: Alice
Enter the marks of student 1: 45
Enter the name of student 2: Bob
Enter the marks of student 2: 32
Enter the name of student 3: Charlie
Enter the marks of student 3: 55

List of failed students:


Name: Bob, Marks: 32
iv) Read with student name and marks from the command line and display the student
details along the total.
Aim: To write a C program to Read with student name and marks from the command line and
display the student details along the total.
Algorithm:
Step1: Check if the correct number of command line arguments is provided.
Step 2: Calculate the number of students based on the command line arguments.
Step 3: Dynamically allocate memory for an array of structures.
Step4: Check if memory allocation is successful. If not, display an error message and exit the
program.
Step 5: Read student names and marks from the command line into the array of structures.
Step 6: Display student details along with the total marks.
Step 7: Free the dynamically allocated memory.
Step 8: Exit the program.
Program:
#include <stdio.h>
#include <stdlib.h>
// Define a structure to represent a student
struct Student
{
char name[50];
int marks;
};

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


{
// Step 1: Check if the correct number of command line arguments is provided
if (argc % 2 != 1 || argc < 3)
{
printf("Usage: %s <name1> <marks1> <name2> <marks2> ... \n", argv[0]);
return 1; // Exit the program with an error code
}
// Step 2: Calculate the number of students based on the command line arguments
int numStudents = (argc - 1) / 2;
// Step 3: Dynamically allocate memory for an array of structures
// Step 4: Check if memory allocation is successful
struct Student *students = (struct Student *)malloc(numStudents * sizeof(struct Student));
if (students == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit the program with an error code
}
// Step 5: Read student names and marks from the command line into the array of structures
for (i = 0; i < numStudents; i++)
{
sscanf(argv[i * 2 + 1], "%s", students[i].name);
sscanf(argv[i * 2 + 2], "%d", &students[i].marks);
}
// Step 6: Display student details along with the total marks
int totalMarks = 0;
printf("\nStudent Details:\n");
for (int i = 0; i < numStudents; i++)
{
printf("Name: %s, Marks: %d\n", students[i].name, students[i].marks);
totalMarks += students[i].marks;
}
// Step 7: Free the dynamically allocated memory
free(students);
// Step 8: Exit the program successfully
return 0;
}
Result:
To compile the program, save it in a file (e.g., student_details.c) and use a C compiler. For
example:
gcc student_details.c -o student_details
Then, run the compiled program with command-line arguments:
./student_details Alice 85 Bob 90 Charlie 78
Student Details:
Name: Alice, Marks: 85
Name: Bob, Marks: 90
Name: Charlie, Marks: 78

Total Marks: 253


v) Write a C program to implement realloc( )
Aim: To write a C program to implement realloc( ).
Algorithm:
Step 1:Input the initial size of the array (n) from the user.
Step 2: Allocate memory for the initial array using malloc( ).
Step 3: Check if memory allocation is successful. If not, display an error message and exit the
program.
Step 4: Input elements for the initial array.
Step 5: Display the elements of the initial array.
Step 6: Input the new size for the array (new Size) from the user.
Step 7: Resize the array using realloc( ).
Step 8: Check if reallocation is successful. If not, display an error message and exit the program.
Step 9: Input additional elements for the resized array.
Step 10: Display the elements of the resized array.
Step11: Free the dynamically allocated memory using free( ).
Step 12: Exit the program.
Program:
#include <stdio.h>
#include <stdlib.h>
int main( )
{
int *arr;
int i, n;
// Step 1: Input the initial size of the array
printf("Enter the initial size of the array: ");
scanf("%d", &n);
// Step 2: Allocate memory for the initial array using malloc()
// Step 3: Check if memory allocation is successful
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit the program with an error code
}
// Step 4: Input elements for the initial array
printf("Enter %d elements:\n", n);
for ( i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Step 5: Display the elements of the initial array
printf("Initial array elements:\n");
for ( i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

// Step 6: Input the new size for the array


int newSize;
printf("\nEnter the new size of the array: ");
scanf("%d", &newSize);
// Step 7: Resize the array using realloc()
// Step 8: Check if reallocation is successful
arr = (int *)realloc(arr, newSize * sizeof(int));
if (arr == NULL)
{
printf("Memory reallocation failed\n");
return 1; // Exit the program with an error code
}

// Step 9: Input additional elements for the resized array


printf("Enter %d additional elements:\n", newSize - n);
for ( i = n; i < newSize; i++)
{
scanf("%d", &arr[i]);
}
// Step 10: Display the elements of the resized array
printf("Resized array elements:\n");
for ( i = 0; i < newSize; i++)
{
printf("%d ", arr[i]);
}
// Step 11: Free the dynamically allocated memory
free(arr);
// Step 12: Exit the program successfully
return 0;
}
Result:
Enter the initial size of the array: 3
Enter 3 elements:
10
20
30
Initial array elements:
10 20 30
Enter the new size of the array: 5
Enter 2 additional elements:
40
50
Resized array elements:
10 20 30 40 50
Conclusion:
That the above program completed and executed successfully and the result obtained correctly.

You might also like