Code
Code
1/ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent).
Place the 40 responses in an integer array and summarize the results of the poll.
#include <stdio.h>
int main() {
int ratings[numStudents];
int total = 0;
double average;
// Input ratings
printf("Enter the ratings of the food (on a scale of 1 to 10) for 40 students:\n");
do {
scanf("%d", &ratings[i]);
// Update total
total += ratings[i];
// Calculate average
// Display summary
return 0;
#include <stdio.h>
#include <string.h>
int main() {
char inputString[MAX_LENGTH];
// Input string
printf("Enter a string: ");
printf("%c", inputString[i]);
printf("\n");
return 0;
3/Write a program that inputs 10 strings that represent floating-point values, converts the strings to double values, sums the values, and prints the
total. Using an array of strings.
#include <stdio.h>
#include <stdlib.h>
int main() {
char inputStrings[10][MAX_STR_LENGTH];
// Input 10 strings
scanf("%s", inputStrings[i]);
sum += value;
return 0;
4/Linear search algorithm finds a key (number) in an array of integers. It searches from the first element to the last element.
Write a linear-search function to receive an array, length of the array, and key as arguments. It returns the index of the first found element?
#include <stdio.h>
int linearSearch(const int array[], int length, int key) {
if (array[i] == key) {
int main() {
int key;
scanf("%d", &key);
if (index != -1) {
} else {
return 0;
5/Create a struct BMI including 2 variables: person’s height and weight in doubles of a student. Print the Body mass index (BMI) value (BMI =
weight/height2, unit is kg/m2) and determine if a person is overweight or not? (For most adults 18-65 years, a BMI of 25.0 or more is overweight,
while the healthy range is 18.5 to 24.9.)
#include <stdio.h>
struct BMI {
};
int main() {
scanf("%lf", &student.height);
scanf("%lf", &student.weight);
// Calculate BMI
if (isOverweight(bmi)) {
} else {
return 0;
6/Write a program that uses the sizeof operator to determine the sizes in bytes of the various data types on your computer system. Write the
results to the file "datasize.dat" so you may print the results later. The format for the results in the file should be as follows (the type sizes on your
computer might be different from those shown in the sample output):
#include <stdio.h>
int main() {
if (file == NULL) {
fclose(file);
return 0;
7/Create a struct Rectangle including 2 variables: width and height in doubles. Print the area and perimeter of the rectangle?
#include <stdio.h>
struct Rectangle {
double width;
double height;
};
int main() {
scanf("%lf", &myRectangle.width);
scanf("%lf", &myRectangle.height);
Structure
Min Max
#include <stdio.h>
int min;
int max;
}MinMax;
int main()
MinMax arrayMinMax;
arrayMinMax = getMinMax(arr,
len);
return 0;
max = arr[i];
min = arr[i];
MinMax arrayMinMax;
arrayMinMax.min = min;
arrayMinMax.max = max;
return arrayMinMax;
1/Write a program that reads three nonzero integer values and determines and prints whether they could represent the sides of a triangle.
#include <stdio.h>
int main() {
scanf("%d", &side1);
scanf("%d", &side2);
scanf("%d", &side3);
if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
} else {
return 0;
Function
#include <stdio.h>
float max = a;
if (b > max) {
max = b;
if (c > max) {
max = c;
if (d > max) {
max = d;
}
return max;
int main() {
scanf("%f", &num1);
scanf("%f", &num2);
scanf("%f", &num3);
scanf("%f", &num4);
return 0;
2/Write a function that takes an integer and returns the sum of its digits
#include <stdio.h>
int sum = 0;
return sum;
int main() {
int number;
// Input an integer
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
Array
1/ Sort in array
int input_array[SIZE];
int input_size;
scanf("%d", &input_size);
input_size; i++) {
printf("Enter
number: ");
int num;
scanf("%d", &num);
input_array[i] = num;
int even_array[SIZE];
int even_size = 0;
int even_list[SIZE];
int odd_array[SIZE];
int odd_size = 0;
int odd_list[SIZE];
int even_count = 0;
int odd_count = 0;
input_size; i++) {
if (input_array[i] % 2
== 0) {
even_array[even_size] =
input_array[i];
even_size++;
even_list[even_count] = i;
even_count++;
else {
odd_array[odd_size] =
input_array[i];
odd_size++;
odd_list[odd_count] = i;
odd_count++;
i++){
input_array[even_list[i]] =
even_array[i];
i++){
input_array[odd_list[i]] = odd_array[i];
printf("result = ");
input_size; i++) {
printf("%d ",
input_array[i]);
printf("");
2/An two-dimensional matrix can be multiplied by another matrix to give a matrix whose elements are the sum of the products of the elements
within a row from the first matrix and the associated elements of a column of the second matrix. Both matrices should either be square matrices,
or the number of columns of the first matrix should equal the number of rows of the second matrix. To calculate each element of the resultant
matrix, multiply the first element of a given row from the first matrix and the first element of a given column in the second matrix, add that to the
product of the second element of the same row and the second element of the same column, and keep doing so until the last elements of the row
and column have been multiplied and added to the sum. Write a program to calculate the product of 2 matrices and store the result in a third
matrix.
#include <stdio.h>
#define MAX_SIZE 10
// Function to multiply two matrices and store the result in a third matrix
printf("Error: Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.\n");
return;
result[i][j] = 0;
printf("%d\t", mat[i][j]);
printf("\n");
int main() {
scanf("%d", &mat1[i][j]);
scanf("%d", &mat2[i][j]);
if (cols1 != rows2) {
printf("Error: Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.\n");
printf("\nMatrix 1:\n");
printf("\nMatrix 2:\n");
printf("\nProduct Matrix:\n");
return 0;
Pointer
Write a function mazeGenerator that takes as an argument a two-dimensional 12-by-12 character array and randomly produces a maze. The
function should also provide the starting and ending locations of the maze.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 12
void mazeGenerator(char maze[SIZE][SIZE], int *startX, int *startY, int *endX, int *endY) {
maze[i][j] = '#';
srand(time(NULL));
printf("\n");
int main() {
char maze[SIZE][SIZE];
printf("Maze:\n");
displayMaze(maze);
// Display the starting and ending locations
return 0;
1/ Write a program that inputs six strings that represent floating-point values, converts the strings to double values, stores the values into a double
array and calculates the sum, and average of the values.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 6
int main() {
char inputStrings[ARRAY_SIZE][100];
double values[ARRAY_SIZE];
scanf("%s", inputStrings[i]);
values[i] = atof(inputStrings[i]);
// Update sum
sum += values[i];
// Calculate average
printf("\nEntered values:\n");
printf("%lf\n", values[i]);
return 0;
2/Write a program that inputs a line of text and counts the total numbers of vowels, consonants, digits and white spaces in the given line of text
#include <stdio.h>
#include <ctype.h>
int main() {
char line[100];
vowels++;
else if ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z')) {
consonants++;
else if (isdigit(currentChar)) {
digits++;
else if (isspace(currentChar)) {
spaces++;
printf("\nResults:\n");
return 0;
File processing
#include <stdio.h>
int main() {
if (file == NULL) {
fclose(file)
return 0;