[go: up one dir, main page]

0% found this document useful (0 votes)
19 views4 pages

Array Problems Solution

The document contains multiple C programming problems, including calculating max, min, and average marks, sorting an array, copying even numbers, managing roll numbers and CGPA, and shifting elements in an array. Each problem is accompanied by a code implementation demonstrating the solution. The document serves as a practical guide for basic programming concepts and array manipulations.

Uploaded by

arafatshitul
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)
19 views4 pages

Array Problems Solution

The document contains multiple C programming problems, including calculating max, min, and average marks, sorting an array, copying even numbers, managing roll numbers and CGPA, and shifting elements in an array. Each problem is accompanied by a code implementation demonstrating the solution. The document serves as a practical guide for basic programming concepts and array manipulations.

Uploaded by

arafatshitul
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/ 4

Problem 2: Max, Min, Average of Marks

#include <stdio.h>

int main() {
int marks, count = 0, max = -1, min = 101, sum = 0;

while (1) {
printf("Enter marks for Student #%d: ", count + 1);
scanf("%d", &marks);
if (marks == -1)
break;

if (marks > max) max = marks;


if (marks < min) min = marks;

sum += marks;
count++;
}

if (count > 0) {
printf("Max. Marks: %d\n", max);
printf("Min Marks: %d\n", min);
printf("Average Marks: %.2f\n", (float)sum / count);
} else {
printf("No marks entered.\n");
}

return 0;
}

Problem 3: Sorting an Array of n Elements

#include <stdio.h>

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

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

// Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

printf("Sorted Array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}

Problem 4: Copy Even Numbers to bx[10]

#include <stdio.h>

int main() {
int ax[10] = {2, 7, 4, 9, 6, 3, 8, 1, 5, 0};
int bx[10], j = 0;

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


if (ax[i] % 2 == 0) {
bx[j] = ax[i];
j++;
}
}

printf("Even numbers copied to bx:\n");


for (int i = 0; i < j; i++)
printf("%d ", bx[i]);

return 0;
}

Problem 5: Roll and CGPA Management

#include <stdio.h>

int main() {
int roll[] = {101, 102, 103, 104, 105, 106};
float cgpa[] = {3.0, 2.2, 3.9, 1.8, 4.0, 2.3};
int n = 6;

// i) Get CGPA by roll


int search_roll = 104;
for (int i = 0; i < n; i++) {
if (roll[i] == search_roll) {
printf("CGPA of roll %d = %.2f\n", search_roll, cgpa[i]);
break;
}
}

// ii) Count CGPA < 2.5


int count = 0;
for (int i = 0; i < n; i++) {
if (cgpa[i] < 2.5) count++;
}
printf("Number of students with CGPA < 2.5: %d\n", count);

// iii) Roll(s) with highest CGPA


float max = cgpa[0];
for (int i = 1; i < n; i++)
if (cgpa[i] > max) max = cgpa[i];

printf("Highest CGPA: %.2f\nRoll Numbers: ", max);


for (int i = 0; i < n; i++)
if (cgpa[i] == max) printf("%d ", roll[i]);
printf("\n");

// iv) Top 5 CGPA with roll numbers (Selection Sort)


for (int i = 0; i < n - 1; i++) {
int maxIdx = i;
for (int j = i + 1; j < n; j++)
if (cgpa[j] > cgpa[maxIdx])
maxIdx = j;

float tmp = cgpa[i];


cgpa[i] = cgpa[maxIdx];
cgpa[maxIdx] = tmp;

int tmpR = roll[i];


roll[i] = roll[maxIdx];
roll[maxIdx] = tmpR;
}

printf("Top 5 Students:\n");
for (int i = 0; i < 5 && i < n; i++)
printf("Roll: %d, CGPA: %.2f\n", roll[i], cgpa[i]);

return 0;
}

Problem 6: Shifting Elements in Array

#include <stdio.h>

int main() {
int ax[100], n = 10;
for (int i = 0; i < n; i++)
ax[i] = i + 1;

// i) Delete element at index 3


for (int i = 3; i < n - 1; i++)
ax[i] = ax[i + 1];
n--;

// ii) Insert 99 at index 5


for (int i = n; i > 5; i--)
ax[i] = ax[i - 1];
ax[5] = 99;
n++;
printf("Updated Array:\n");
for (int i = 0; i < n; i++)
printf("%d ", ax[i]);

return 0;
}

You might also like