EE110: Programming For Engineers - 1: Sections A, B, C (Fall 2011)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

National University of Computer & Emerging Sciences

EE110: Programming for Engineers 1


Sections A, B, C (Fall 2011)
Thursday 22 Sep 2011 Instructors: Aamer Munir / Shahid Qureshi Lab Engineers: Attique Dawood, Hina Ashraf TAs: Awais Aslam, Taimoor Naveed (Batch08)

A04
Friday 30 Sep 2011

IMPORTANT NOTES:
Please read all the sections below and understand them well.

Assignments related:
1. Do the assignment yourself. It is a lot better to attempt an assignment yourself and get low marks then to cheat and get full marks. Know why! 2. ZERO Tolerance Policy for plagiarism and cheating. First cheating case gets zero. Subsequent cheating case gets ZERO in all assignments. 3. All assignments carry equal marks. 4. No late submission. 5. There is no retake or allocation of average marks for assignments in any case if you fail to submit because of whatever reason, genuine or otherwise. 6. Overall best N-1 assignments out of N will be considered.

Coding guidelines:
1. The code should be properly formatted for easy readability. Insert spaces and new lines to improve reading. 2. Every file that you submit must have the following block of comments (appropriately changed) on top of it. The code should start below this block. /*/////////////////////////////////////////////////// Course: EE110 - Programming for Engineers - 1 Semester: Fall 2011 --------------------------------------------------Assignment No: 04 Date: 30 Sep 2011 Roll No: 11i-0007 Section: A Name: Dr. Watson --------------------------------------------------How to use this program: 1. Describe how this program should be used. ///////////////////////////////////////////////////*/ 3. The variable naming convention is xNameOfVariable, where x specifies type of variable, 'n' or 'i' for int, 'f' for float, 'd' for double, 'c' for char and 'str' for string or array of characters. Note how the variable name is itself a detailed description of what the variable is used for. Main idea is it is not too short neither a story in its own name.

NU-FAST: Fall11: Prog-1

Page 1/6

Submission guidelines
Submissions failing to follow these guidelines will not be considered.

1. Submit only one ZIP file. No re-submission will be possible this time. 2. The ZIP file must be named as Fall11_11i-0007_a04.zip 3. The ZIP file must contain all the relevant assignment files. Each file must conform to the Fall11_11i-0007_Qx.cpp or appropriate naming standard (in case the file is not C code file, its extension must be appropriate). Where x is the question no. 4. Submit on SLATE. 5. Submit before 7:30 am on the due date.

Assignment Problems
1. [1 Mark] Run the following code on your computer. Understand in detail as to what it is doing. Specifically give consideration to the use of: a. %3d (note how the 3 is affecting the output. Change it to 6, 8 etc and see the difference). b. \t in printf. You have to answer the following questions: i. Why nothing is printed with 0, 7, 8, 9, 10, 32, 255. ii. Why is 10 not printed in its logical place. iii. Why the second line contain only 10? iv. Where are 11, 12, 13? Why they arent printed? v. Why is equal to sign not printed with 8?
#include <stdio.h> void main (void) { printf("\n\n"); // prints as number and as character for(int n = 0; n<256; n++) printf("%3d=%c\t", n, n); // prints as number and as character printf("\n\n"); // prints as number and as character }

NU-FAST: Fall11: Prog-1

Page 2/6

2. [3 Marks] The following program asks the user to enter marks (0 100) and then find and prints how many were <50, =50 and >50. Study the program, run it on your computer and debug it if needed to understand its flow. You are required to modify the program to use do-while loop instead of the for loop used. Also please note the use of #define preprocessor directive. Note how we can change the threshold to some other value by making a single change instead of making 9 changes within the code! By changing the value of THRESHOLD. Similarly note the other #define directives.
#include <stdio.h> #define THRESHOLD #define MAX_COUNT #define DATA_TERMINATOR 50 20 -1

void main(void) { printf("Find how many marks entered by user are <%d, =%d and >%d.\n", THRESHOLD, THRESHOLD, THRESHOLD); printf("Marks are assumed to be positive and from 0 - 100\n"); printf("Total count must be less than %d.\n", MAX_COUNT); printf("Enter '%d' after your last number!\n", DATA_TERMINATOR); printf("Type in a number and press ENTER.\n"); int nEntNo = 0; //int to store entered value int nLT, nET, nGT; nLT = nET = nGT = 0; //receive values from the user for(int i = 0; i < MAX_COUNT; i++) { scanf("%d", &nEntNo); if(nEntNo == DATA_TERMINATOR) break; if(nEntNo > THRESHOLD) nGT++; else if(nEntNo == THRESHOLD) nET++; else nLT++; } printf("Marks <%d are %d\nMarks =%d are %d\nMarks >%d are %d\n", THRESHOLD, nLT, THRESHOLD, nET, THRESHOLD, nGT); }

NU-FAST: Fall11: Prog-1

Page 3/6

3. [3 marks] In the program of question # 2 (above), find a way to increment the various counts (nLT, nET, nGT) using switch and cases? if, else if, and else etc cannot be used! [Hint: You may not be able to do it directly on the data received. You may need to
change the incoming data such that all numbers <50 become one value, =50 one value, and >50 one value. There may be other variants. In programming there is always multiple ways to achieve the same result.]

NU-FAST: Fall11: Prog-1

Page 4/6

4. [3 Marks] What are the GREEN numbers in the following program? Please note the use of the code segments/statements in BOLD RED color. Please learn how to write C code. One of the best ways to learn is to look at written codes.
[How to do it: Copy the code section only and paste in a blank new file in MS Visual Studio. Save the file as assign4-1.cpp file. Press F7 to compile (click all YES). Press CTRL+F5 to run or F5 to run in debug mode. Press F9 with cursor on a line to mark a breakpoint at that line. Press F10 to single step through the code. Single stepping means executing one line at a time.] #include <stdio.h> #include <conio.h> main() { int nIndex = 1; char strFail[] = "Sorry: This is not the GREEN number."; char strPass[] = "Bravo! This is the GREEN number!"; do { printf("\n*********************************************\n"); printf("********* AAMER'S CALCULATING ENGINE ********\n"); printf("*********************************************\n\n"); printf("Please enter a positive integer to check: "); unsigned int nNum; scanf("%ud", &nNum); unsigned int nTemp = nNum; if(nTemp%2 == 0) { printf("%s\n", strFail); } else if(nTemp == 1) { printf("%s\n", strPass); } else { while(nTemp-=2) { if(nTemp <= 1) { printf("%s\n", strPass); break; } if(nNum%nTemp == 0) { printf("%s\n", strFail);

NU-FAST: Fall11: Prog-1

Page 5/6

break; } } } printf("Do you wish to check another number? Y/N: "); char ch = getche(); //this function is declared in conio.h printf("\n"); switch(ch) { case 'y': case 'Y': continue; break; default: //note the order of cases printf("Considering your intention as 'NO'!\n"); case 'n': case 'N': printf("Thank you for using the program! Bye.\n"); nIndex = 0; break; } } while(nIndex); }

NU-FAST: Fall11: Prog-1

Page 6/6

You might also like