Question 01
Write a C program that finds the maximum value in a predefined array of integers. Let the array be equal to {10, 3, 45, 21, 7, 88, 32, 15}.
-
Use a
forloop to traverse the array. -
Use an
ifstatement to check and update the current maximum value.
Answer 01
#include <stdio.h>
void main()
{
int size = 8;
// Array declaration
int arr[size] = {10, 3, 45, 21, 7, 88, 32, 15};
// Declare max and set it to first element
int max = arr[0];
// Loop through the array
for (int i = 1; i < size; i++)
{
// Compare array content against max
if (arr[i] > max)
{
// Update max
max = arr[i];
}
}
// Print max value to check
printf("Maximum value in the array is: %d\n", max);
}
Question 02
Write a C program that calculates the sum of all numbers between 1 and 100 (inclusive) that are divisible by 5.
Answer 02
#include <stdio.h>
void main()
{
// declare the sum variable and initialize it to 0
int sum = 0;
// loop through all the numbers 1.100
for (int i = 1; i <= 100; i++)
{
// if the number is a mutiple of 5
if (i % 5 == 0)
{
// add it to sum
sum = sum + i;
}
}
printf("Sum of numbers divisible by 5 between 1 and 100: %d", sum);
}
Question 03
Write a C program that copies an array of integers into another array. Your program should have an original array and a copy array. Make the array equal to {0, 7, 8, 10, -4, 5, 6, 9, 11, -32}.
Answer 03
#include <stdio.h>
void main()
{
// Declare the array
int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
// Declare the copy
int arrCopy[10];
// Copy the array
for (int i = 0; i < 10; i++)
{
arrCopy[i] = arr[i];
}
// Print the copied array
printf("Copy of array: ");
for (int i = 0; i < 10; i++)
{
printf("%d ", arrCopy[i]);
}
printf("\n");
}
Question 04
Write a C program that copies an array of integers into another array in reverse order. Your program should have an original array and a reversed array. Make the array equal to {0, 7, 8, 10, -4, 5, 6, 9, 11, -32}.
Answer 04
#include <stdio.h>
void main()
{
// Declare the array
int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
// Declare the array that holds the reversed copy
int arrMirror[10];
// Copy the array in reverse order
for (int i = 0; i < 10; i++)
{
arrMirror[i] = arr[10-i-1];
}
// Print reversed array copy
printf("Reversed copy of array: ");
for (int i = 0; i < 10; i++)
{
printf("%d ", arrMirror[i]);
}
printf("\n");
}
Question 05
Write a C program that reverses an array of integers in place i.e. you should not create another array. Make the array equal to {0, 7, 8, 10, -4, 5, 6, 9, 11, -32}.
Answer 05
#include <stdio.h>
void main()
{
// Declare the array
int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
// We wuill use this when we swap numbers
int temp;
// Reverse the content of the array
// Loop only until half of the array
// Looping through the whole array will reverse,
// then, reverse reverse i.e. bring back to
// original position
for (int i = 0; i < 5; i++)
{
int temp = arr[i];
arr[i] = arr[10-i-1];
arr[10-i-1] = temp;
}
// Print reversed array
printf("Reversed array: ");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
Question 06
Write a C program that finds the second largest number in an array of integers.
Answer 06
#include <stdio.h>
#include <stdio.h>
void main() {
// Declare the array
int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
//int arr[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
// Initialize largest and second largest to the first element
int largest = arr[0];
int secondLargest = arr[0];
// Find largest and second largest
for (int i = 1; i < 10; i++)
{
if (arr[i] > largest)
{
// Update second largest
secondLargest = largest;
// Update largest
largest = arr[i];
}
else if (arr[i] < largest && arr[i] > secondLargest)
{
// Update second largest
secondLargest = arr[i];
}
}
// Check if second largest was updated
if (largest == secondLargest)
{
printf("There is no second largest number in the array.\n");
}
else
{
printf("The second largest number is: %d\n", secondLargest);
}
}