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 (Batch’08)

Friday 30 Sep 2011

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: Fall’11: Prog-1

Page 1/6

n++) printf("%3d=%c\t". 12. Where are 11. n). %3d (note how the 3 is affecting the output.Submission guidelines Submissions failing to follow these guidelines will not be considered. \t in printf. // prints as number and as character printf("\n\n"). n<256. Where x is the question no. Submit before 7:30 am on the due date. Why nothing is printed with 0. 13? Why they aren’t printed? v. 8.h> void main (void) { printf("\n\n"). [1 Mark] Run the following code on your computer. No re-submission will be possible this time.cpp or appropriate naming standard (in case the file is not C code file. 255. 4. ii. Submit on SLATE. // prints as number and as character for(int n = 0. 10. You have to answer the following questions: i. Why is 10 not printed in its logical place. Understand in detail as to what it is doing. its extension must be appropriate). b. 32. 1. 7. 9. iii. // prints as number and as character } NU-FAST: Fall’11: Prog-1 Page 2/6 . Why the second line contain only 10? iv. 5. Submit only one ZIP 3. The ZIP file must be named as Fall11_11i-0007_a04. Each file must conform to the Fall11_11i-0007_Qx. Specifically give consideration to the use of: a. The ZIP file must contain all the relevant assignment files. Change it to 6. n. 8 etc and see the difference). Why is ‘equal to’ sign not printed with 8? #include <stdio. 2. Assignment Problems 1.

} NU-FAST: Fall’11: Prog-1 Page 3/6 . &nEntNo). 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. else if(nEntNo == THRESHOLD) nET++. =%d and >%d. printf("Total count must be less than %d. nLT = nET = nGT = 0.\n". #include <stdio. else nLT++. THRESHOLD. //int to store entered value int nLT. MAX_COUNT). } printf("Marks <%d are %d\nMarks =%d are %d\nMarks >%d are %d\n". nET.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. Similarly note the other #define directives. run it on your computer and debug it if needed to understand its flow. nLT. You are required to modify the program to use do-while loop instead of the for loop used. THRESHOLD). if(nEntNo > THRESHOLD) nGT++. THRESHOLD. printf("Enter '%d' after your last number!\n". nGT. i++) { scanf("%d". if(nEntNo == DATA_TERMINATOR) break. nGT). THRESHOLD. Also please note the use of #define preprocessor directive. DATA_TERMINATOR). int nEntNo = 0. THRESHOLD. printf("Marks are assumed to be positive and from 0 . [3 Marks] The following program asks the user to enter marks (0 – 100) and then find and prints how many were <50.100\n"). printf("Type in a number and press ENTER. nET.2.\n". //receive values from the user for(int i = 0. THRESHOLD.\n"). Study the program. =50 and >50. i < MAX_COUNT.

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

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

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

Sign up to vote on this title
UsefulNot useful