Solutions to Tutorial Questions for Arrays, Functions and Files-1
Solutions to Tutorial Questions for Arrays, Functions and Files-1
FILES
#include <stdio.h>
// Function to check if a number is prime
int isprime(int num) {
if (num <= 1){
return 0; // Numbers less than 2 are not prime
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0){
return 0; // Not prime
}
}
return 1; // Prime
}
int main() {
int start, end, i;
// Input range from the user
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
printf("Prime numbers between %d and %d are:\n", start, end);
for (i = start; i <= end; i++) {
if (isprime(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0; }
#include <stdio.h>
// Function to rearrange the array
void rearrange(int arr[], int n) {
int temp[n], index = 0, i;
// Collect all positive numbers
for (i = 0; i < n; i++) {
if (arr[i] >= 0) {
temp[index++] = arr[i];
}
}
// Collect all negative numbers
for (i = 0; i < n; i++) {
if (arr[i] < 0) {
temp[index++] = arr[i];
}
}
// Copy back to the original array
for (i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
int main() {
int n;
3. write a function check(x, y, n) that returns 1 if both x and y follow between 0 and n-1,
inclusive. The function should return 0 otherwise assume that x, y, and n are all of type
integers. Write a main program to test this function.
#include <stdio.h>
// Function to check if x and y are within the range [0, n-1]
int check(int x, int y, int n) {
if(x >= 0 && x < n && y >= 0 && y < ){
return 1;
}
else{
return 0;
}
}
int main() {
int x, y, n;
// Input values from the user
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the values of x and y: ");
scanf("%d %d", &x, &y);
// Test the function and display the result
if (check(x, y, n)) {
printf("Both x and y are within the range [0, %d-1].\n", n);
} else {
printf("x or y is out of the range [0, %d-1].\n", n); } return 0; }
#include <stdio.h>
int main() {
int n, i;
// Input array size
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n], sum = 0;
float average;
// Input array elements
printf("Enter the elements of the array:\n");
for ( i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i]; // Calculate the sum
}
// Calculate average
average = (float)sum / n;
// Display results
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", average);
return 0;
}
5. write a program to reverse the elements of a one-dimensional array without using
another array.
#include <stdio.h>
int main() {
int n, i;
// Input array size
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Input array elements
printf("Enter the elements of the array:\n");
for ( i = 0; i < n; i++) {
#include <stdio.h>
int main() {
int n, i, j;
// Input array size
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Input array elements
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find and display duplicate elements
printf("Duplicate elements are:\n");
for ( i = 0; i < n; i++) {
for ( j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
5 out of 11 | P a g e Prepared by: mwanamapambano & allysalumu
printf("%d\n", arr[i]);
break;
}
}
}
return 0;
}
7. write a program to add two 2x2 matrices and display the resultant matrix.
#include <stdio.h>
int main() {
int mat1[2][2], mat2[2][2], result[2][2], i, j;
// Input first matrix
printf("Enter elements of the first 2x2 matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &mat1[i][j]);
}
}
return 0;
}
#include <stdio.h>
int main() {
int mat1[2][2], mat2[2][2], result[2][2], i, j;
// Input first matrix
printf("Enter elements of the first 2x2 matrix:\n");
for ( i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &mat1[i][j]);
}
}
// Input second matrix
printf("Enter elements of the second 2x2 matrix:\n");
for ( i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &mat2[i][j]);
}
}
// Subtract the matrices
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
// Display the result
printf("Resultant matrix after subtraction:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
// Function to convert Fahrenheit to Celsius
float fahrenheitToCelsius(float fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
// Function to convert Celsius to Fahrenheit
float celsiusToFahrenheit(float celsius) {
return (celsius * 9 / 5) + 32;
}
int main() {
float temp;
// Test Fahrenheit to Celsius
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &temp);
printf("Temperature in Celsius: %.2f\n", fahrenheitToCelsius(temp));
// Test Celsius to Fahrenheit
printf("Enter temperature in Celsius: ");
scanf("%f", &temp);
printf("Temperature in Fahrenheit: %.2f\n", celsiusToFahrenheit(temp));
return 0;
}
10. Write a function isLeapYear(int year) that checks whether a given year is a leap year.
Use it to display all leap years in a given range.
#include <stdio.h>
// Function to check if a year is a leap year
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}
return 0;
}
int main() {
int start, end, i;
// Input range from the user
printf("Enter the start year: ");
scanf("%d", &start);
printf("Enter the end year: ");
scanf("%d", &end);
struct Student {
char name[50];
int id;
float marks;
};
int main() {
FILE *file;
struct Student s;
int n, i;
scanf("%d", &n);
if (file == NULL) {
fclose(file);
if (file == NULL) {
printf("\nStudent Records:\n");
fclose(file);
return 0;}
12. Write a program to read the content of a text file and copy it into another file.
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
// Open source file for reading
source = fopen("source.txt", "r");
if (source == NULL) {
printf("Error opening source file or no such file\n");
}
// Open destination file for writing
destination = fopen("destination.txt", "w");
if (destination == NULL) {
10 out of 11 | P a g e Prepared by: mwanamapambano & allysalumu
printf("Error opening destination file or no such file");
fclose(source);
}
// Copy contents from source to destination
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
printf("File copied successfully.\n");
fclose(source);
fclose(destination);
return 0;}
NOTE: you are free to edit or change any code as more as you can in order to meet your
needs