WEEK – 9
1. Write a C program to find the sum of a 1D array using malloc()
Algorithm
1. Start
2. Declare an integer pointer arr and variables n, i, sum.
3. Input the number of elements n.
4. Allocate memory for n integers using malloc():
5. arr = (int *) malloc(n * sizeof(int));
o If memory allocation fails, print an error and exit.
6. Input n elements into the allocated array.
7. Initialize sum = 0.
8. Loop through the array and calculate the sum:
9. sum += arr[i];
10. Print the sum.
11. Free the allocated memory using free(arr).
12. End
#include <stdio.h>
#include <stdlib.h> // for malloc() and free()
int main() {
int n, i;
int *arr; // pointer for dynamic array
int sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
arr = (int *) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1; // exit program
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of array elements = %d\n", sum);
// free allocated memory
free(arr);
return 0;
}
Sample Output
Example 1
Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50
Sum of array elements = 150
Example 2
Enter number of elements: 3
Enter 3 elements:
5 15 25
Sum of array elements = 45
2. Write a C program to find the total, average of n students using structures
Algorithm
1. Start
2. Define a struct Student with fields: name (string) and marks (integer).
3. Input the number of students n.
4. Create an array of structures: struct Student s[n].
5. Initialize total = 0.
6. Input data for each student:
o Input name
o Input marks
o Add marks to total.
7. Calculate average = total / n.
8. Display results:
o For each student, print name and marks.
o Print total and average marks.
9. End
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
int n, i, total = 0;
float average;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student s[n]; // array of structures
for (i = 0; i < n; i++) {
printf("\nEnter name of student %d: ", i + 1);
scanf("%s", s[i].name);
printf("Enter marks of %s: ", s[i].name);
scanf("%d", &s[i].marks);
total += s[i].marks;
}
average = (float) total / n;
printf("\n---- Results ----\n");
for (i = 0; i < n; i++) {
printf("%s got %d marks\n", s[i].name, s[i].marks);
}
printf("\nTotal marks of %d students = %d\n", n, total);
printf("Average marks = %.2f\n", average);
return 0;
}
Sample Output
Example 1
Enter number of students: 3
Enter name of student 1: Alice
Enter marks of Alice: 85
Enter name of student 2: Bob
Enter marks of Bob: 90
Enter name of student 3: Charlie
Enter marks of Charlie: 78
---- Results ----
Alice got 85 marks
Bob got 90 marks
Charlie got 78 marks
Total marks of 3 students = 253
Average marks = 84.33
Example 2
Enter number of students: 2
Enter name of student 1: John
Enter marks of John: 70
Enter name of student 2: Mary
Enter marks of Mary: 80
---- Results ----
John got 70 marks
Mary got 80 marks
Total marks of 2 students = 150
Average marks = 75.00
3. C program to Enter n students data using calloc() and display failed students list
Algorithm
1. Start
2. Define struct Student with name and marks.
3. Input the number of students n.
4. Allocate memory dynamically using calloc:
5. s = (struct Student *) calloc(n, sizeof(struct Student));
6. Input student data for each student:
o Name
o Marks
7. Loop through the array and check for failed students (marks < 40).
8. Print the name and marks of failed students.
9. Free the allocated memory using free(s).
10. End
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int marks;
};
int main() {
int n, i;
struct Student *s;
printf("Enter number of students: ");
scanf("%d", &n);
s = (struct Student *) calloc(n, sizeof(struct Student));
for (i = 0; i < n; i++) {
printf("\nEnter name of student %d: ", i + 1);
scanf("%s", s[i].name);
printf("Enter marks: ");
scanf("%d", &s[i].marks);
}
printf("\nFailed Students (marks < 40):\n");
for (i = 0; i < n; i++) {
if (s[i].marks < 40) {
printf("%s - %d\n", s[i].name, s[i].marks);
}
}
free(s);
return 0;
}
Sample Output
Example 1
Enter number of students: 4
Enter name of student 1: Alice
Enter marks: 35
Enter name of student 2: Bob
Enter marks: 50
Enter name of student 3: Charlie
Enter marks: 20
Enter name of student 4: David
Enter marks: 70
Failed Students (marks < 40):
Alice - 35
Charlie - 20
Example 2
Enter number of students: 3
Enter name of student 1: John
Enter marks: 45
Enter name of student 2: Mary
Enter marks: 30
Enter name of student 3: Peter
Enter marks: 25
Failed Students (marks < 40):
Mary - 30
Peter - 25
4. C program to Read student name and marks from the command line and display the
student details along with the total.
ALGORITHM:
Step 1: Start
Step 2: Accept command line arguments (argc, argv[])
argv[1] → student name
argv[2] ... argv[n] → marks
Step 3: Check if the number of arguments is less than 3
If yes, print usage message and stop
Step 4: Store the student name from argv[1]
Step 5: Initialize total = 0
Step 6: Loop through arguments from argv[2] to argv[argc-1]
Convert each string to integer using atoi()
Add marks to total
Step 7: Print student name, all marks, and total
Step 8: Stop
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i, total = 0;
if (argc < 3) {
printf("Usage: %s <name> <mark1> <mark2> ... <markN>\n", argv[0]);
return 1;
}
printf("Student Name: %s\n", argv[1]);
printf("Marks: ");
for (i = 2; i < argc; i++) {
int mark = atoi(argv[i]);
printf("%d ", mark);
total += mark;
}
printf("\nTotal Marks = %d\n", total);
return 0;
}
5. Write a C program to implement realloc()
Algorithm to Implement realloc()
Problem: Dynamically allocate an array, input elements, then resize it using realloc().
Algorithm:
1. Start
2. Declare a pointer for dynamic memory (e.g., int *arr) and variables for size and loop
index.
3. Ask the user to enter the initial number of elements n.
4. Allocate memory using malloc(n * sizeof(int)).
5. Check if memory allocation is successful. If not, exit.
6. Input n elements from the user and store in the array.
7. Display the array elements.
8. Ask the user for the new size new_n.
9. Reallocate memory using realloc(arr, new_n * sizeof(int)).
10. Check if realloc() is successful. If not, exit.
11. If new_n > n, input additional elements for the new positions.
12. Display all new_n elements.
13. Free the allocated memory using free().
14. End
C Program Using realloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n, new_n, i;
printf("Enter initial number of elements: ");
scanf("%d", &n);
arr = (int *) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter new size of array: ");
scanf("%d", &new_n);
arr = (int *) realloc(arr, new_n * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
if (new_n > n) {
printf("Enter %d new elements:\n", new_n - n);
for (i = n; i < new_n; i++) {
scanf("%d", &arr[i]);
}
}
printf("New array: ");
for (i = 0; i < new_n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Example 1
Enter initial number of elements: 3
Enter 3 elements:
10 20 30
Original array: 10 20 30
Enter new size of array: 5
Enter 2 new elements:
40 50
New array: 10 20 30 40 50
Example 2
Enter initial number of elements: 4
Enter 4 elements:
5 15 25 35
Original array: 5 15 25 35
Enter new size of array: 2
New array: 5 15