0% found this document useful (0 votes)
7 views5 pages

Parth Pals1

The document contains two C programs that demonstrate dynamic memory allocation. The first program calculates and displays the average and highest marks of students, while the second program processes a user-input sentence to count characters, vowels, and spaces. Both programs utilize functions for input and calculations, and ensure proper memory management by freeing allocated memory.

Uploaded by

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

Parth Pals1

The document contains two C programs that demonstrate dynamic memory allocation. The first program calculates and displays the average and highest marks of students, while the second program processes a user-input sentence to count characters, vowels, and spaces. Both programs utilize functions for input and calculations, and ensure proper memory management by freeing allocated memory.

Uploaded by

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

Parth palse

Tut 9

EXCP P7-2

49

Q1) Write a C program that: Dynamically allocates memory to store marks of n


students using malloc().

a) Takes input for each student’s marks. (Using Input Function)


b) Calculates and displays: (Using functions): Average marks, Highest marks
c) Frees the allocated memory using free().

Constraints:
Number of students (n) is entered by the user.
Use pointers to access array elements.

Code

#include <stdio.h>

#include <stdlib.h>

void inputMarks(int *marks, int n) {

for (int i = 0; i < n; i++) {

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

scanf("%d", &marks[i]);

float calculateAverage(int *marks, int n) {

int sum = 0;

for (int i = 0; i < n; i++) {

sum += marks[i];

return (float)sum / n;
}

int findHighest(int *marks, int n) {

int highest = marks[0];

for (int i = 1; i < n; i++) {

if (marks[i] > highest) {

highest = marks[i];

return highest;

int main() {

int n;

printf("Enter number of students: ");

scanf("%d", &n);

int *marks = (int *)malloc(n * sizeof(int));

if (marks == NULL) {

printf("Memory allocation failed!\n");

return 1;

inputMarks(marks, n);

printf("Average Marks = %.2f\n", calculateAverage(marks, n));

printf("Highest Marks = %d\n", findHighest(marks, n));

free(marks);

return 0;

Output
Q2) Write a C program that takes a sentence from the user using dynamic memory
allocation. The program should: Dynamically allocate memory to store the sentence
using malloc.

a) Count and display:


Total number of characters (excluding \0)
Total number of vowels in the sentence
Total number of spaces in the sentence

b) Use a user-defined function for:


Input of sentence
Counting vowels
Counting spaces

c) Free the allocated memory at the end.

Code

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

void inputSentence(char *str, int size) {

printf("Enter a sentence: ");

getchar();

fgets(str, size, stdin);

}
int countVowels(char *str) {

int count = 0;

for (int i = 0; str[i] != '\0'; i++) {

char ch = tolower(str[i]);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

count++;

return count;

int countSpaces(char *str) {

int count = 0;

for (int i = 0; str[i] != '\0'; i++) {

if (str[i] == ' ') {

count++;

return count;

int main() {

int size;

printf("Enter maximum size of sentence: ");

scanf("%d", &size);

char *sentence = (char *)malloc(size * sizeof(char));

if (sentence == NULL) {

printf("Memory allocation failed!\n");

return 1;

}
inputSentence(sentence, size);

int vowelCount = countVowels(sentence);

int spaceCount = countSpaces(sentence);

int charCount = 0;

while (sentence[charCount] != '\0' && sentence[charCount] != '\n') {

charCount++;

printf("Total number of characters (excluding \\0): %d\n", charCount);

printf("Total number of vowels: %d\n", vowelCount);

printf("Total number of spaces: %d\n", spaceCount);

free(sentence);

return 0;

Output

You might also like