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

Assignment 3 C Programs

This document contains five C programming assignments focused on array manipulation. The tasks include finding maximum and minimum values, calculating averages, searching for items, sorting arrays, copying arrays, and converting decimal numbers to binary. Each assignment is accompanied by example code demonstrating the required functionality.

Uploaded by

hunkerunknown686
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)
4 views4 pages

Assignment 3 C Programs

This document contains five C programming assignments focused on array manipulation. The tasks include finding maximum and minimum values, calculating averages, searching for items, sorting arrays, copying arrays, and converting decimal numbers to binary. Each assignment is accompanied by example code demonstrating the required functionality.

Uploaded by

hunkerunknown686
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

Introduction to Computing Lab - Assignment 3

1. Write a C program for the following: Store a list of integers in a one dimensional array and print t

(a) The maximum value within the array.


(b) The minimum value within the array.
(c) The average value.
(d) Find the sum of odd numbers within the array.
(e) Print the array contents in reverse order.

#include <stdio.h>

int main() {
int arr[100], n, i, max, min, sum = 0, odd_sum = 0;
float avg;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d integers:\n", n);


for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
if(arr[i] % 2 != 0)
odd_sum += arr[i];
}

max = min = arr[0];


for(i = 1; i < n; i++) {
if(arr[i] > max) max = arr[i];
if(arr[i] < min) min = arr[i];
}

avg = (float)sum / n;

printf("Maximum value: %d\n", max);


printf("Minimum value: %d\n", min);
printf("Average value: %.2f\n", avg);
printf("Sum of odd numbers: %d\n", odd_sum);

printf("Array in reverse order:\n");


for(i = n-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

2. Write a C program to search a data-item in the array, if it exists in the array, print the index.
Introduction to Computing Lab - Assignment 3

#include <stdio.h>

int main() {
int arr[100], n, i, key, found = 0;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d integers:\n", n);


for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter item to search: ");


scanf("%d", &key);

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


if(arr[i] == key) {
printf("Item found at index: %d\n", i);
found = 1;
break;
}
}

if(!found)
printf("Item not found.\n");

return 0;
}

3. Write a C program that can sort n numbers stored in an one dimensional array. The list of numbe

#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d integers:\n", n);


for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

for(i = 0; i < n-1; i++) {


for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
Introduction to Computing Lab - Assignment 3

arr[j+1] = temp;
}
}
}

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

return 0;
}

4. Write a C program that can copy the list of data item from one array to another array.

#include <stdio.h>

int main() {
int arr1[100], arr2[100], n, i;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d integers:\n", n);


for(i = 0; i < n; i++)
scanf("%d", &arr1[i]);

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


arr2[i] = arr1[i];

printf("Copied array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr2[i]);
printf("\n");

return 0;
}

5. Write a C program that can print the binary equivalent of a decimal integer. Here decimal integer

#include <stdio.h>

void toBinary(int n) {
if(n > 0) {
toBinary(n / 2);
printf("%d", n % 2);
}
}
Introduction to Computing Lab - Assignment 3

int main() {
int num;

printf("Enter a decimal number: ");


scanf("%d", &num);

if(num == 0)
printf("Binary: 0\n");
else {
printf("Binary: ");
toBinary(num);
printf("\n");
}

return 0;
}

You might also like