Tutorial 11 & 12
Find the minimum and maximum of sequence of 10 numbers stored in array.
#include<stdio.h>
int main(){
int numbers[10]={12,15,34,56,87,62,78,90,47,69};
int i, min=numbers[0], max=numbers[0];
for(i=1;i<10;++i){
max=numbers[i]>max?numbers[i]:max;
min=numbers[i]<min?numbers[i]:min;
}
printf("Maximum number is %i\nMinimum number is %i\n", max, min);
return 0;
}
User Entered 10 Numbers
#include<stdio.h>
int main(){
int numbers[10];
int i, min, max;
for(i=0;i<10;++i){
printf("Enter the number : ");
scanf("%i", &numbers[i]);
}
min=numbers[0], max=numbers[0];
for(i=1;i<10;++i){
max=numbers[i]>max?numbers[i]:max;
min=numbers[i]<min?numbers[i]:min;
}
printf("\nMaximum number is %i\nMinimum number is %i\n", max, min);
return 0;
}
Find the total and average of a sequence of 10 numbers stored in array.
#include<stdio.h>
int main(){
int numbers[10];
int i;
float total=0;
for(i=0;i<10;++i){
printf("Enter the number : ");
scanf("%i", &numbers[i]);
}
for(i=0;i<10;++i){
total+=numbers[i];
}
printf("\nTotal is %.2f\nAverage is %.2f\n", total, (total/10));
return 0;
}
Following list represents the data units used by three persons. Create an array to store these details and print the list.
#include<stdio.h>
int main(){
int i,j,max,min, total=0, person;
int unit[3][3]={
{500,1000,300},
{10,1800,200},
{200,400,700}
};
char* name[]={"work","mobile","home"};
/* Print table */
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]);
}
/* Find total unit 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);
/* Find total units for each 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");
/* 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);
/* 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);
/* 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);
/* 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;
}
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.
#include<stdio.h>
int main(){
float marks[10][6];
float maxForMid, maxForPor, maxForESE;
int count[6];
int i,j,tot;
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");
}
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);
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;
}
Assume a village has 20 houses. Input electricity unit charges and calculate total electricity bill
#include<stdio.h>
int main(){
int i,j;
float bill, units;
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;
}
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");
}
for(i=0;i<20;++i){
bill=0;
bill+=pay[i][2];
}
printf("The total earnings of the month %.2f\n", bill);
for(i=0;i<20;++i){
units=0;
units+=pay[i][0];
}
printf("The total units consumed %.2f\n", units);
return 0;
}
Display all prime numbers between two Intervals using a function.
#include<stdio.h>
void primeNumbers(int n, int m);
int main(){
int n,m;
printf("Enter two numbers : ");
scanf("%i %i", &n, &m);
primeNumbers(n, m);
return 0;
}
void primeNumbers(int n, int m){
int i,j, c;
for(i=n;i<m;++i){
c=0;
for(j=1;j<=i;++j){
if((i%j)==0)
c++;
}
if(c==2)
printf("%i\t", i);
}
printf("\n");
return;
}
Find sum of natural numbers using a recursive function.
#include<stdio.h>
int nature(int n);
int main(){
printf("%i\n", nature(100));
return 0;
}
int nature(int n){
return n>0?n+nature(n-1):0;
}
Calculate the power of a number using a recursive function.
#include<stdio.h>
double powerCalculate(int number, int power);
int main()
{
printf("%.0f\n", powerCalculate(2,5*2));
return 0;
}
double powerCalculate(int number, int power){
return power>1?number*powerCalculate(number, power-1):number;
}
Write a function to return the trip cost which calculated using the given distance in kilometers. Note: Take 35 LKR as travelling cost per kilometer.
#include<stdio.h>
double tripCost(float km);
int main(){
printf("%.2fLKR\n", tripCost(100.5));
return 0;
}
double tripCost(float km){
return km*35;
}
Write a function to convert the LKR currency into US dollars.
#include<stdio.h>
double lkrConverter(float lkr);
int main(){
printf("$%.2f\n", lkrConverter(320));
return 0;
}
double lkrConverter(float lkr){
return lkr*0.0032;
}
Write a function to input electricity unit charges and calculate total electricity bill
according to the given condition:
a. For first 50 units Rs. 0.50/unit b. For next 100 units Rs. 0.75/unit
c. For next 100 units Rs. 1.20/unit
d. For unit above 250 Rs. 1.50/unit
#include<stdio.h>
double calculateBill(int units);
int main(){
printf("%.2fLKR\n", calculateBill(270));
return 0;
}
double calculateBill(int units){
return (units<=50?units*0.5:units<=150?((units-50)*0.75)+25:units<=250?((units-150)*1.2)+100:((units-250)*1.5)+220)*1.2;
}
Write a program for both main line and internal process of the flowcharts
#include<stdio.h>
void leaderEarnings();
int main(void){
int i=0;
while(i<20){
leaderEarnings();
i++;
}
return 0;
}
void leaderEarnings(){
char empId[10];
int NoOfItemsSold;
float unitPrice, leaderEarning=0, commission=0;
printf("Enter employee number : ");
scanf("%s", &empId);
printf("Enter number of items sold : ");
scanf("%i", &NoOfItemsSold);
printf("Enter unit price of the item : ");
scanf("%f", &unitPrice);
leaderEarning=unitPrice*NoOfItemsSold;
commission=leaderEarning*0.5;
printf("\n%s is earn %.2f\n\n", empId, commission);
return;
}
Modify and re-write the program written in Question 8 to print the total amount paid as the weekly sales commission for all sales leaders.
#include<stdio.h>
double leaderEarnings();
int main(void){
int i=0;
float totalCommission=0;
while(i<20){
totalCommission+=leaderEarnings();
i++;
}
printf("Total Commission of all sales leaders %.2f\n", totalCommission);
return 0;
}
double leaderEarnings(){
char empId[10];
int NoOfItemsSold;
float unitPrice, leaderEarning=0, commission=0;
printf("Enter employee number : ");
scanf("%s", &empId);
printf("Enter number of items sold : ");
scanf("%i", &NoOfItemsSold);
printf("Enter unit price of the item : ");
scanf("%f", &unitPrice);
leaderEarning=unitPrice*NoOfItemsSold;
commission=leaderEarning*0.5;
printf("\n%s is earn %.2f\n\n", empId, commission);
return commission;
}
Suppose that the management of this organization has decided to allow sales leaders to sell more than one different item as they wish. Modify and re-write the program written in Question 9 to satisfy this requirement. Note: 50% of total sales earnings is paid as the sales commission for a sales leader.
#include<stdio.h>
double leaderEarnings();
int main(void){
int i=0;
float totalCommission=0;
while(i<20){
totalCommission+=leaderEarnings();
i++;
}
printf("Total Commission of all sales leaders %.2f\n", totalCommission);
return 0;
}
double leaderEarnings(){
char empId[10];
int NoOfItemsSold, i, n;
float unitPrice, leaderEarning, commission=0;
printf("Enter employee number : ");
scanf("%s", &empId);
printf("How many items does sale? ");
scanf("%i", &n);
for(i=0;i<n;++i){
leaderEarning=0;
printf("Enter number of items sold : ");
scanf("%i", &NoOfItemsSold);
printf("Enter unit price of the item : ");
scanf("%f", &unitPrice);
leaderEarning=unitPrice*NoOfItemsSold*0.5;
commission+=leaderEarning;
}
printf("\n%s is earn %.2f\n\n", empId, commission);
return commission;
}
Comments