In this article, we are gonna to discuss questions to expert Arrays
Before doing the tutorial 11, watch this videos to get an idea about Arrays
Here you can find the tutorial 11 questions pdf to practice Arrays ;
https://www.thecn.com/uploads/647160/6471604970875b5c6e006e27/s3/NjMzNWEyM2JhOGE1NWUxZjk5NWIzNTcxLzY0NzE2MDQ0NjIwMTIyZDBmYjAzZGM5MC9JQ1QxNDAyX1R1dG9yaWFsXzExLnBkZg/ef21958ec14fdbd99a004511285cc100.pdf
1. Type in and run all programs presented in lecture note 11. Briefly describe the theory/
concept you learned from these programs separately.
- Describing the C arrays.
- Practicing the declaration, initialization, and access linear arrays.
- Practicing the declaration, initialization, and access two-dimensional arrays.
- Applying taught concepts for writing programs.
2. Find the minimum and maximum of sequence of 10 numbers stored in array.
Answer
#include <stdio.h>
int main() {
int numbers[10];
int i;
int min, max;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &numbers[i]);
}
min = max = numbers[0];
for (i = 1; i < 10; i++)
{
if (numbers[i] < min)
{
min = numbers[i];
}
if (numbers[i] > max)
{
max = numbers[i];
}
}
printf("Minimum: %d\n", min);
printf("Maximum: %d\n", max);
return 0;
}
03.Find the total and average of a sequence of 10 numbers stored in array.
Answers
#include <stdio.h>
int main()
{
float Total,Average;
int i;
float marks[10]={70,90,88,56,98,95,93,94,92,83};
Total=0;
Average=0;
for (i=0;i<10;i++)
{
Total=Total+marks[i];
}
printf("Total=%.2f\n",Total);
Average=Total/10;
printf("Average=%.2f",Average);
return 0;
}
4. Following list represents the data units used by three persons.
Create an array to store these details and print the list.
person work mobile home
1 500 1000 300
2 10 1800 200
3 200 400 700
Find the following using the above table;
I. Find the total unit consumed.
II. Find the total consumption of units for each phone type.
III. Who consumed maximum unites for mobile phones?
IV. Who pays highest phone bill?
V. Who pays lower phone bill?
VI. How many units are consumed by person 3?
Answer
#include<stdio.h>
int main(){
int i,j,max,min, person;
int total=0;
int unit[3][3]={
{500,1000,300},
{10,1800,200},
{200,400,700}
};
char* name[]={"work","mobile","home"};
printf("+--------+------+--------+------+\n| PERSON | WORK | MOBILE | HOME |\n+--------+------+--------+------+\n");
for(i=0;i<3;++i)
{
printf("|%8i|%6i|%8i|%6i|\n+--------+------+--------+------+\n", (i+1), unit[i][0], unit[i][1], unit[i][2]);
}
/*I)Find the total units consumed*/
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
total+=unit[i][j];
}
}
printf("\nThe total unit consumed is %i\n\n", total);
/*II) Find the total consumption of units for each phone type. */
for(i=0;i<3;++i)
{
total=0.0;
for(j=0;j<3;++j)
{
total+=unit[j][i];
}
printf("The total consumption of units for %s is %i\n", name[i],total);
}
printf("\n");
/*III) Who consumed maximum unites for mobile phones? */
max=unit[0][1];
for(i=1;i<3;++i)
{
max=unit[i][1]>max?unit[i][1]:max;
}
printf("Maximum unites for %s is %i\n\n", name[1],max);
/*IV) Who pays highest phone bill? */
max=unit[0][0]+unit[0][1]+unit[0][2];
person=1;
for(i=1;i<3;++i)
{
total=0;
for(j=0;j<3;++j)
{
total+=unit[i][j];
}
person=total>max?i+1:person;
}
printf("%i pays highest phone bill\n\n",person);
/*V) Who pays lower phone bill? */
min=unit[0][0]+unit[0][1]+unit[0][2];
person=1;
for(i=1;i<3;++i)
{
total=0;
for(j=0;j<3;++j)
{
total+=unit[i][j];
}
person=total<min?i+1:person;
}
printf("%i pays lower phone bill\n\n",person);
/*VI)How many units are consumed by person 3?*/
total=0;
for(i=0;i<3;++i)
{
total+=unit[2][i];
}
printf("Person 3 consumed %i units\n", total);
return 0;
}
5.Write a program to store students’ marks for COM1407 subject. Assume there are 10 students in the class. You have to store marks in a 2D array as shown below.
I. Display detailed mark sheet
II. Find the highest marks for midterm examination, portfolio work and end semester
examination separately
III. Find the student count based on the following criteria
a. Rounded Marks >= 90% : Grade A
b. Rounded Marks >= 80% : Grade B
c. Rounded Marks >= 70% : Grade C
d. Rounded Marks >= 60% : Grade D
e. Rounded Marks >= 40% : Grade E
f. Rounded Marks < 40% : Grade F
Your output should be displayed as follows
Grade Count
A
B
C
D
E
Answer
#include<stdio.h>
int main(){
float marks[10][6];
float maxForMid, maxForPor, maxForESE;
int count[6];
int i,j,tot;
/*I)Display detailed mark sheet*/
for(i=0;i<10;++i){
printf("Enter Mid term examination marks : ");
scanf("%f", &marks[i][0]);
printf("Enter Portfolio marks : ");
scanf("%f", &marks[i][1]);
printf("Enter End semester examination marks : ");
scanf("%f", &marks[i][2]);
printf("Enter Final Marks marks : ");
scanf("%f", &marks[i][3]);
marks[i][4]=marks[i][0]+marks[i][1]+marks[i][3];
marks[i][5]=(int)(marks[i][2]+marks[i][4])/2;
printf("\n\n");
}
printf("+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+\n");
printf("| Mid term exam | Portfolio | End semester exam | Final Marks | Total | Rounded Marks |\n");
printf("+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+\n");
for(i=0;i<10;++i){
for(j=0;j<6;++j){
printf("|%19.2f",marks[i][j]);
}
printf("|\n");
printf("+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+\n");
}
/*II) Find the highest marks for midterm examination, portfolio work and end semester examination separately*/
maxForMid=marks[0][0];
maxForPor=marks[0][1];
maxForESE=marks[0][2];
for(j=1;j<10;++j){
maxForMid=marks[i][0]>maxForMid?marks[i][0]:maxForMid;
maxForPor=marks[i][1]>maxForPor?marks[i][1]:maxForPor;
maxForESE=marks[i][2]>maxForESE?marks[i][2]:maxForESE;
}
printf("highest marks for midterm examination %.2f\n", maxForMid);
printf("highest marks for Portfolio %.2f\n", maxForPor);
printf("highest marks for End semester examination %.2f\n", maxForESE);
/*III) Find the student count based on the following criteria*/
for(i=0;i<10;++i){
if(marks[i][5]>=90){
count[0]++;
}else if(marks[i][5]>=80){
count[1]++;
}else if(marks[i][5]>=70){
count[2]++;
}else if(marks[i][5]>=60){
count[3]++;
}else if(marks[i][5]>=40){
count[4]++;
}else{
count[5]++;
}
}
printf("+-------+-------+\n| Grade | Count |\n+-------+-------+\n");
printf("| A |%7i|\n| B |%7i|\n| C |%7i|\n| D |%7i|\n| E |%7i|\n| F |%7i|\n+-------+-------+\n", count[0], count[1], count[2],count[3], count[4], count[5]);
return 0;
}
6.Assume a village has 20 houses. Input electricity unit charges and calculate total electricity bill
I. Write a program to store house address, units consumed, surcharge and total
amount separately in arrays.
II. Display the detailed monthly consumption report of the village using the
following order
Serial Number: House Address Units Surcharge Amount to be paid
III. Calculate the total earnings of the month
IV. Calculate the total units consumed
Answer
#include<stdio.h>
int main(){
int i,j;
float bill, units;
/*I) Write a program to store house address, units consumed, surcharge and total amount separately in arrays. */
char* details[20][2];
float pay[20][3];
for(i=0;i<2;++i){
printf("Enter Serial Number : ");
scanf("%s", &details[i][0]);
printf("Enter House Address : ");
scanf("%s", &details[i][1]);
printf("Enter Units : ");
scanf("%i", &pay[i][0]);
printf("\n");
}
for(i=0;i<20;++i)
{
units=pay[i][0];
bill = (units<=50?units*0.5:units<=150?((units-50)*0.75)+25:units<=250?((units-150)*1.2)+100:((units-250)*1.5)+220);
pay[i][1]=bill*0.2;
pay[i][2]=bill*1.2;
}
/*II) Display the detailed monthly consumption report of the village*/
printf("+---------------+---------------+-------+-----------+-------------------+\n");
printf("| Serial Number | House Address | Units | Surcharge | Amount to be paid |\n");
printf("+---------------+---------------+-------+-----------+-------------------+\n");
for(i=0;i<20;++i){
printf("|%15s|%15s|%7.0f|%11.2f|19.2f|\n", details[i][0], details[i][1], pay[i][0], pay[i][1], pay[i][2]);
printf("+---------------+---------------+-------+-----------+-------------------+\n\n");
}
/*III) Calculate the total earnings of the month*/
for(i=0;i<20;++i)
{
bill=0;
bill+=pay[i][2];
}
printf("The total earnings of the month %.2f\n", bill);
/*IV) Calculate the total units consumed*/
for(i=0;i<20;++i)
{
units=0;
units+=pay[i][0];
}
printf("The total units consumed %.2f\n", units);
return 0;
}
7. Extend the following program that you did in Tutorial 05 to store the processed bills in a
buffer.
Develop a program to a cashier system for a shop.
Your system should display following prompt for the cashier and the entire program is
controlled according to the command given by the cashier.
Welcome to <shop name>
Press S Start
Press Q Quit
Press P Process Bill
Your option : _
Once you press S: System will display welcome message and ready for processing a bill
by initializing required variables.
Once you press Q: You can shut down the cashier system.
Once you press P: Process the current customer’s shopping cart. Here, you can enter the
item code, qty, unit price of the items in shopping cart and calculate and display the total
Once you finished serving to the current customer, you can press N to move to the
prompt.
Answer
#include<stdio.h>
int main()
{
int i=0;
char input,input1,input2;
char itemC[100][10];
float unit_p[100],qty[100],subtotal[100];
float total=0.0;
printf("Welcome to Basu's Mart\n");
printf("-------------------\n");
printf("Press S to Start\n");
printf("Press Q to Quit\n");
do
{
scanf(" %c",&input);
switch(input)
{
case 'S':
printf("Press P to Process bill\n");
scanf(" %c",&input1);
do
{
switch(input1)
{
case 'P':
while(1)
{
printf("Enter item code or (Press T to proceed to Total) : ");
scanf("%s",&itemC[i]);
if (strcmp(itemC[i], "T") == 0)
break;
printf("Enter quantity : ");
scanf("%f",&qty[i]);
printf("Enter Unit price LKR : ");
scanf("%f",&unit_p[i]);
subtotal[i] = qty[i]*unit_p[i];
total = total + subtotal[i];
i++;
}
printf("Item code Quantity unit price Sub total\n");
printf("-------------------------------------------\n");
for(--i;i>=0;--i)
{
printf("%8s %7.0f %9.2f LKR %7.2f LKR\n",itemC[i],qty[i],unit_p[i],subtotal[i]);
}
printf("\nTotal = %8.2f LKR\n",total);
total = 0.0;
i=0;
break;
default:
printf("\nInvalid input\nPress S to Start\nPress Q to Quit\n");
break;
}
if(input1 != 'P')
break;
printf("Press N to proceed to next customer\n");
scanf(" %c",&input2);
}while(input2 =='N');
printf("\nInvalid input\nPress S to Start\nPress Q to Quit\n");
case 'Q':
break;
default :
printf("\nInvalid input\nPress S to Start\nPress Q to Quit\n");
break;
}
}while (input != 'Q');
return 0;
}
8. Design a program for payroll department to calculate the monthly payment of 10
employees. Use arrays appropriately. Finally you need to display 10 pay sheets one after
the other.
Answer
#include <stdio.h>
typedef struct {
int employee_id;
float hourly_rate;
float hours_worked;
} Employee;
int main() {
char option;
Employee employees[10];
float total_pay;
int i;
do {
printf("Welcome to the Payroll Department\n");
printf("Press S to start, Q to quit, or P to process payroll: ");
scanf(" %c", &option);
switch (option)
{
case 'S':
printf("Initializing payroll system.\n");
for (i = 0; i < 10; i++)
{
printf("Enter details for employee %d:\n", i + 1);
printf("Employee ID: ");
scanf("%d", &employees[i].employee_id);
printf("Hourly rate: ");
scanf("%f", &employees[i].hourly_rate);
printf("Hours worked: ");
scanf("%f", &employees[i].hours_worked);
}
break;
case 'P':
printf("Processing payroll...\n");
printf("Pay Sheets:\n");
for (i = 0; i < 10; i++)
{
float total_pay = employees[i].hourly_rate * employees[i].hours_worked;
printf("Employee ID: %d, Total Pay: $%.2f\n", employees[i].employee_id, total_pay);
}
break;
case 'Q':
printf("Exiting payroll system. Goodbye!\n");
break;
default:
printf("Invalid option. Please try again.\n");
break;
}
} while (option != 'Q');
return 0;
}