0% found this document useful (0 votes)
52 views8 pages

Birla Institute of Technology & Science, Pilani, Hyderabad Campus First Semester 2020-2021 Computer Programming (CS F111) Lab 5

1. The document discusses local and global variables in C programming. Local variables are declared within a function and are only accessible within that function. Global variables are declared outside of functions and can be accessed throughout the entire program. 2. The document also discusses enum data types in C, which allow the programmer to define an enumeration and assign names to integral constants to make the code more readable. Enumerations have default integer values but these can be explicitly set. 3. Practice problems include programs to print a multiplication table, perform arithmetic operations based on user input, and calculate distance traveled by a vehicle based on velocity, acceleration, and time intervals.

Uploaded by

D V SASANKA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views8 pages

Birla Institute of Technology & Science, Pilani, Hyderabad Campus First Semester 2020-2021 Computer Programming (CS F111) Lab 5

1. The document discusses local and global variables in C programming. Local variables are declared within a function and are only accessible within that function. Global variables are declared outside of functions and can be accessed throughout the entire program. 2. The document also discusses enum data types in C, which allow the programmer to define an enumeration and assign names to integral constants to make the code more readable. Enumerations have default integer values but these can be explicitly set. 3. Practice problems include programs to print a multiplication table, perform arithmetic operations based on user input, and calculate distance traveled by a vehicle based on velocity, acceleration, and time intervals.

Uploaded by

D V SASANKA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Birla Institute of Technology & Science, Pilani, Hyderabad Campus

First Semester 2020-2021

Computer Programming [CS F111] Lab 5

I. Local vs global variable.

Variables that are declared inside a function or block are called local variables. They can
be used only by statements that are inside that function or block of code. Local variables are not
known to functions outside their own. The following example shows how local variables are
used. Here all the variables a, b, and c are local to main() function.

#include <stdio.h>
int main () {
/* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}

Output: value of a = 10, b = 20 and c = 30

Global variables are defined outside a function, usually on top of the program. Global
variables hold their values throughout the lifetime of your program and they can be accessed
inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration. The following program show how global
variables are used in a program.

#include <stdio.h>
/* global variable declaration */
int g;
int main () {
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
Output: value of a = 10, b = 20 and g = 30

A program can have same name for local and global variables but the value of local variable
inside a function will take preference. Here is an example −

#include <stdio.h>
/* global variable declaration */
int g = 20;
int main () {
/* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}
Output: value of g = 10

II. Enum data-type

Enumeration is a user-defined datatype in C language. It is used to assign names to the


integral constants which make a program easy to read and maintain. The keyword “enum” is
used to declare an enumeration.
Here is the syntax of enum in C language,

enum enum_name{const1, const2, ....... };


Eg.: enum flag {const1, const2, ..., constN};

By default, ​const1​ is 0, ​const2​ is 1 and so on. You can change default values of enum elements
during declaration (if necessary).

// Changing default values of enum constants


enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3,
}

There are two ways to define the variables of enum type as follows.

enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday};


enum week day;

Here is an example of enum in C language.

#include<stdio.h>

enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};

enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};

int main() {

printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue, Wed, Thur,

Fri, Sat, Sun);

printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond , Tues, Wedn,

Thurs, Frid, Satu, Sund);

return 0;

Output: ​The value of enum week: 10 11 12 13 10 16 17


The default value of enum day: 0 1 2 3 18 11 12
Practice Problems:
1. Write a program to print the multiplication table in the following format

#include <stdio.h>
int main()
{
int multi1,multi2;
printf ("Multiplication Table from 1 to 15\n\n" );
printf (" " );

for (multi2 = 1 ; multi2 <= 5; multi2++ )


printf("%5d" ,multi2);
printf(" \n" );

for (multi1 = 1 ; multi1 <= 5; multi1++ )


{
printf ("%2d" ,multi1);
for (multi2 = 1 ; multi2 <=5; multi2++ )
printf("%5d" , multi1 * multi2);
printf(" \n" );
}

return 0;
}

2. ​Write a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result.
(Consider the operators +,-,*, /, % and use Switch Statement)

Solution:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,res,ch;
clrscr();
printf("\t *********************");
printf("\n\tMENU\n");
printf("\t********************");
printf("\n\t(1)ADDITION");
printf("\n\t(2)SUBTRACTION");
printf("\n\t(3)MULTIPLICATION");
printf("\n\t(4)DIVISION");
printf("\n\t(5)REMAINDER");
printf("\n\t(0)EXIT");
printf("\n\t********************");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);

if(ch<=5 & ch>0)


{
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
}

switch(ch)
{
case 1:
res=a+b;
printf("\n Addition:%d",res);
break;

case 2:
res=a-b;
printf("\n Subtraction:%d",res);
break;

case 3:
res=a*b;
printf("\n Multiplication:%d",res);
break;

case 4:
res=a/b;
printf("\n Division:%d",res);
break;

case 5:
res=a%b;
printf("\n Remainder:%d",res);
break;

case 0:
printf("\n Choice Terminated");
exit();
break;

default:
printf("\n Invalid Choice");
}
getch();
}

​ 3.​ The total distance traveled by vehicle in 't' seconds is given by distance = ut+1/2at2
where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
Write C program to find the distance traveled at regular intervals of time given the values of
'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals
and repeat the calculations for different values of 'u' and 'a'.

Solution:

#include <stdio.h>
#include <math.h>
void main()
{
int tim_intrval, counter,time;
float accl, distance=0, velos;
clrscr();
printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A
VEHICLE===========>");
printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");
scanf("%d",&tim_intrval);

for(counter = 1; counter <= tim_intrval; counter++)


{
printf("\n\t\t\tAT T%d TIME(sec) : ",counter);
scanf("%d",&time);
printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time);
scanf("%f",&velos);
printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time);
scanf("%f",&accl);
distance += (velos*time + (accl*pow(time,2))/2);
}

printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME :


%f",tim_intrval,distance);
getch();
}

Exercise:
1. An electric power distribution company charges its domestic customers as follows:
Consumption Units Rate of charge
0-200 Rs 0.50 per unit
201-400 Rs 100 plus Rs 0.65 per unit excess 200
401-600 Rs 230 plus Rs 0.80 per unit excess 400
Write a C program that reads the customer number and power consumed and prints the
amount to be paid by the customer.

2. What is the output for the below code

#include<stdio.h>
main()
{
int *j;
{
int i=10;
j=&i;
}
printf("%d",*j);
}

NOTE: ​Upload the screenshots of the ​Exercise programs ​along with the displayed results into
your corresponding Google Classroom.
PATH to Submit the Screenshots​:
Google Classroom --> Classwork --> View Assignment --> Create/Upload

You might also like