[go: up one dir, main page]

0% found this document useful (0 votes)
15 views6 pages

WEEK9

Uploaded by

jcsp1126
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)
15 views6 pages

WEEK9

Uploaded by

jcsp1126
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/ 6

WEEK-9

1)Write a C program to find the sum of a 1D array using malloc()

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

int main() {
int *arr, size, sum = 0;

// Input the size of the array


printf("Enter the number of elements: ");
scanf("%d", &size);

// Allocate memory for the array


arr = (int *)malloc(size * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

// Input the elements of the array


printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Calculate the sum of the array elements


for (int i = 0; i < size; i++) {
sum += arr[i];
}

// Display the sum


printf("Sum of the array elements = %d\n", sum);

// Free the allocated memory


free(arr);

return 0;
}

Enter the number of elements: 4


Enter the elements of the array:
2222
Sum of the array elements = 8

2)Write a C program to find the total, average of n students using


structures

#include <stdio.h>

typedef struct {
char name[50];
float marks[5];
float total;
float average;
} Student;

int main() {
int n;
Student students[100];

// Input the number of students


printf("Enter the number of students: ");
scanf("%d", &n);

// Input student data and calculate total and average


for (int i = 0; i < n; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", students[i].name);

students[i].total = 0;
for (int j = 0; j < 5; j++) {
printf("Enter marks for subject %d: ", j + 1);
scanf("%f", &students[i].marks[j]);
students[i].total += students[i].marks[j];
}
students[i].average = students[i].total / 5;
}

// Display total and average for each student


printf("\nStudent Details:\n");
for (int i = 0; i < n; i++) {
printf("Name: %s\n", students[i].name);
printf("Total Marks: %.2f\n", students[i].total);
printf("Average Marks: %.2f\n", students[i].average);
printf("\n");
}

return 0;
}

3) WRITE A C PROGRAM TO Enter n students data using calloc() and


display failed students list
#include <stdio.h>
#include <stdlib.h>

typedef struct {
char name[50];
float marks;
} Student;

int main() {
Student *students;
int n;

// Input the number of students


printf("Enter the number of students: ");
scanf("%d", &n);

// Allocate memory for students data


students = (Student *)calloc(n, sizeof(Student));
if (students == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

// Input student data


for (int i = 0; i < n; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter marks of student %d: ", i + 1);
scanf("%f", &students[i].marks);
}

// Display failed students


printf("\nFailed Students List:\n");
for (int i = 0; i < n; i++) {
if (students[i].marks < 40) {
printf("Name: %s, Marks: %.2f\n", students[i].name,
students[i].marks);
}
}

// Free the allocated memory


free(students);

return 0;
}

Enter the number of students: 2


Enter name of student 1: N
Enter marks of student 1: 11
Enter name of student 2: MN
Enter marks of student 2: 45

Failed Students List:


Name: N, Marks: 11.00

4)WRITE A C PROGRAM TO Read student name and marks


from the command line and display the student details along with
the total.
#include <stdio.h>
#include <stdlib.h>

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


if (argc != 6) {
printf("Usage: %s <name> <mark1> <mark2> <mark3>
<mark4>\n", argv[0]);
return 1;
}

char *name = argv[1];


float mark1 = atof(argv[2]);
float mark2 = atof(argv[3]);
float mark3 = atof(argv[4]);
float mark4 = atof(argv[5]);
float total = mark1 + mark2 + mark3 + mark4;

printf("Student Name: %s\n", name);


printf("Marks: %.2f, %.2f, %.2f, %.2f\n", mark1, mark2, mark3,
mark4);
printf("Total Marks: %.2f\n", total);

return 0;
}

5)WRITE A C PROGRAM TO Implement realloc()

#include <stdio.h>
#include<stdlib.h>
void main()
{
int *ptr,i,n;
printf("enter n:");
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(int));
printf("\nenter %d elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
printf("\nthe elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",*(ptr+i));
}
printf("\nnow reallocate:");
ptr=realloc(ptr,(n-2)*sizeof(int));
printf("\nthe elements are:");
for(i=0;i<n-2;i++)
{
printf("\n%d",*(ptr+i));
}
printf("\nnow deallocate:");
free(ptr);
printf("\nelements are:");
for(i=0;i<n-2;i++)
{
printf("\n%d",*(ptr+i));
}
}
enter n:4
enter 4 elements:1 2 3 4

the elements are:


1
2
3
4
now reallocate:
the elements are:
1
2
now deallocate:
elements are:
0
0

WEEK7 PROGRAM4
O/P:
Output:
Enter the number of bits do you want to enter :8

Enter the binary number : 11110000


11110000
The ones complement of the binary number is :00001111
The twos complement of a binary number is : 00010000

WEEK6 PROGRAM3:
OUTPUT:
Enter the angle in radians: 57.26
Enter the number of terms: 2
sin(57.26) = -31232.539529
cos(57.26) = -1638.353800

You might also like