22cs2037 Lab1
22cs2037 Lab1
CS211 LAB 1
Programming in C: A Recap
th
of February of the year 1600. Beingborn
Q1. Count Dracula was born on the 29
on this specific date, he can’t celebrate his birthday every year. Can youcalculatehow many
birthdays he managed to celebrate till the year 2022.
⇒#include <stdio.h>
int main() {
int year;
int count=0;
for(year=1600;year<=2022;year++)
if (year % 4==0&& year%100!=0){
count++;
}
printf("%d\n",count);
return 0;
}
::
Q2. Write a program to simulate a game of rock-paper-scissors. Theprogramshould allow the
user to enter their choice and display the computer's choice. Itshould use the game rules to
determine the winner.
⇒ code:
⇒
⇒ output:
::
You should enter your choice as rock, paper or scissor. The computer shouldusethe rand()
function to generate a random number between 0, 1 or 2todetermine its choice( use number =
rand()%3; to generate a randomnumberbetween 0, 1, 2). Assign these random value choices as
lets say 0 = rock, 1=paper or 2 = scissor.
After each round the program should ask to continue or exit like question1.
CPU won.
Press Y to play again or N to exit
Bonus: you can also keep track of the total number of rounds played andthewinning score
of the user and CPU.
Q3. You are designing a security system for a bank. The systemshouldhaveapassword-protected
door that allows only authorized personnel to enter. Writeaprogram that prompts the user for
a password and grants access if the passwordis correct.
⇒#include <stdio.h>
int main() {
char pass[100]="sunny123";
char inpass[100];
printf("Enter password\n");
scanf("%s",inpass);
if(strcmp(pass,inpass)==0){
printf("access granted\n");
else{
printf("wrong password
return 0;
::
Q4. You are designing a vending machine that dispenses snacks. Themachine accepts
coins and notes of various denominations. Write a programthatcalculates the total value
of the coins and bills inserted by the user anddispensesthe appropriate snack.
⇒ #include <stdio.h>
#define SNACK_PRICE 100
int main() {
int coins, notes, totalAmount;
if (change > 0) {
printf("Change: %.2f\n", (float)change);
}
} else {
printf("Insufficient amount. Please insert more money.\n");
}
return 0;
}
:: >>
Q5. Here you can assume the price of your snacks (say 29 rs or 37 rs etc) andif auser insert
more than required money then it should return in appropriatemanner.( example if for a 37 rs
snack, I entered 50 rs, then the systemshouldreturn me 13 rs as 10 rs + 2 rs + 1 rs).
⇒#include <stdio.h>
int main() {
scanf("%d", &snackPrice);
scanf("%d", &amountPaid);
returnChange(snackPrice, amountPaid);
} else {
return 0;
}
::
Q 6. You stored all of your friends' heights in an array and you want tofindthetop three
tallest and top three smallest friends of your group.
Q 7. Can you arrange all of your friends according to the increasing order of theirheights?
Q 8. Write a C program which allows you to Do following operations over agivenarray using
functions (develop a function for each task).
1. Create the array. Here the array size and elements should be enteredbytheuser. 2.
Traverse elements in the array. Your program should traverse thewholearray or find out
the value stored at an index given by the user. 3. Insert elements into the array. The
elements should be inserted at a. At the end of array
b. At the given index by the user
c. The Array is sorted (create a sorted array of your choice) andthenthe new
elements to be inserted should be provided by theuser. 4. Delete elements from the
array.
a. Delete from the index provided by the user.
b. Delete the element ( you have to first find the element
inthearray)provided by the user.
⇒
Q 9. Write a program with a for loop. This loop should run N number of times. Dothe
following.
1. Use clock() function to calculate the time taken to executetheprogram. 2.
Varry the value of N from 1 to 1 million and calculate theexecutiontime for each
value of N. Plot a graph, and observehowtheexecution time affected with
respect to the value of N.
Q 10. Try to create an array of 1 million entries. See what errors youmayencounter.
( tip: normally arrays are stored in static memory so small size is available, trytostore
it in dynamic memory for very large arrays.)