Programming Assignment 2
Programming Assignment 2
Programming Assignment # 2
Deadline: Friday April 08, 2011 11:59 PM Submission: Using LMS Question# 1: Create a function in C/C++, which takes two numbers from user and checks their validity according to the following criteria. Provide a menu to user so that he/she can play the game for more than once using do while loop: a. The number should not be a prime number. b. Each number should be between 1 and 1000 c. The two numbers should not be divisible by each other. d. The difference of the two should not be less than 1. e. The addition of these two numbers should not exceed 1000. Provide proper customized error (reason for invalidity) checking if the above criteria is not met. Question#2: A right triangle can have three sides (b=base, p=perpendicular and h=hypotenuse) that are all integers. A triangle is said to be the right triangle if it fulfills the Pythagoras theorem. Write a function using nested loops to find out all right triangles where b, p and h are smaller than number N. Your function should take N as an argument (input). Also write a main program to test this function. Pythagoras theorem states: h2 = b2 + p2 Question#3: Create an automated register for a bakery. Create four integers and name them accordingly for four items (e.g. bread, eggs, milk and butter). Associate a variable to each item, indicating the amount of a particular item present in the bakery at the start of a day and assign each item a unit cost (e.g 1 bread = 40, 1 egg = 4, 1 liter milk = 34 and 1 kg butter = 25). The user sees a menu in the start that shows him the number of each item in the bakery. The user also has an option to sell any number of these items. Keep a track of the total sales the user makes. The user can view the remaining items and the total sale he has achieved (through menu option). When user wants to exit the program, he should see statistics of the items remaining and the total sales done. Also make sure to check for errors, wrong input and any other bugs in the program. Note: Your approach to solve this problem is important. Modular approach (using functions) will win more points.
Question#4 (Bonus Question): Swap two integers without using any other variable. Code to swap two variables using a temp variable follows: #include<stdio.h> int main(){ int temp=0;int a=3; int b=8; printf("The two numbers before swapping are:%d and %d\n",a,b); temp = a; a = b; b = temp; printf("The two numbers after swapping become:%d and%d\n",a,b); return 0; }