0% found this document useful (0 votes)
7 views18 pages

22cs2037 Lab1

The document contains a series of programming exercises focused on C language, including tasks such as calculating leap years, simulating a rock-paper-scissors game, designing a password-protected security system, and creating a vending machine program. It also includes exercises for managing arrays, calculating execution time, and handling large data sets. Each question is accompanied by example code snippets and expected functionalities.
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)
7 views18 pages

22cs2037 Lab1

The document contains a series of programming exercises focused on C language, including tasks such as calculating leap years, simulating a rock-paper-scissors game, designing a password-protected security system, and creating a vending machine program. It also includes exercises for managing arrays, calculating execution time, and handling large data sets. Each question is accompanied by example code snippets and expected functionalities.
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/ 18

Rajeev Gandhi Institute of Petroleum Technology, Jais Amethi Data

Structures and Algorithms

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.

Example: User choice: paper


CPU choice : rock // based on the random number generated

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

entered\n"); printf("try again\n");

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;

printf("Welcome to the Snack Vending Machine!\n");


printf("Snack Price: %.2f\n", (float)SNACK_PRICE);
printf("Insert coins: ");
scanf("%d", &coins);

printf("Insert notes: ");


scanf("%d", &notes);

totalAmount = coins + notes;

printf("Total amount inserted: %.2f\n", (float)totalAmount);


if (totalAmount >= SNACK_PRICE) {
int change = totalAmount - SNACK_PRICE;

printf("Here's your snack!\n");

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>

void returnChange(int price, int amountPaid) {

int change = amountPaid - price;

int denominations[] = {10, 5, 2, 1};

printf("Change to be returned: %d rs\n", change);


for (int i = 0; i < 4; i++) {

int count = change / denominations[i];


if (count > 0) {

printf("%d rs: %d notes\n", denominations[i],

count); change -= count * denominations[i];

int main() {

int snackPrice, amountPaid;

printf("Enter the price of the snack: ");

scanf("%d", &snackPrice);

printf("Enter the amount paid: ");

scanf("%d", &amountPaid);

if (amountPaid >= snackPrice) {

returnChange(snackPrice, amountPaid);

} else {

printf("Insufficient amount paid.\n");


}

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.)

You might also like