Assignment 1 + Solution

pdf

School

Humber College *

*We aren’t endorsed by this school

Course

104

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

13

Uploaded by ChiefCrown4969

Report
Computer Programming, I Fall 2021 1 Assignment 1 + Solution Individual/independent Due Date: OCT 17, 2021 Assignment 1: 15 points of total mark Assignment 1 consist of two sections: Part1: Answering questions [40%] Part2: Coding (Lab) [60%] For the coding you could work with TAs during lab sessions. Please consider that TAs will help you about your problems and error, you can not ask them about solution. How submit your solution for assignment 1: Part1: For the first part submit your solution as a single PDF file and upload on courselink. You can type or take picture scan your solutions and upload it with following name patter Name_FamilyName_StudentID_LAB1_PART1.pdf Par2: Take screen shot of the output (source code is not necessary) of each program and submit it as a single file with following name pattern. Name_FamilyName_StudentID_LAB1_PART2.pdf Upload the source code of each program with a following pattern: Name_FamilyName_StudentID_LAB1_PROGRAMX.c Ex: AMIN_SAFAEI_123456_LAB1_PROGRAM1.c
Computer Programming, I Fall 2021 2 Assignment 1 - Part1 1. Categorize each of the following items as either hardware or software: (5 Points) a. CPU ANS: Hardware. b. C++ compiler ANS: Software. c. ALU ANS: Hardware. d. C++ preprocessor ANS: Software. e. input unit ANS: Hardware. f. an editor program ANS: Software. 2. Discuss the meaning of each of the following names: (5 Points) a. stdin ANS: stdin (the standard input stream), which is normally the keyboard, but stdin can be connected to another stream. b. stdout ANS: Data is often output to stdout (the standard output stream), which is normally the computer screen, but stdout can be connected to another stream. c. stderr ANS: The standard error stream is referred to as stderr. The stderr stream (normally connected to the screen) is used for displaying error messages. It’s common to route regular output data, i.e., stdout, to a device other than the screen while keeping stderr assigned to the screen so that the user can be immediately informed of errors. 3. Identify and correct the errors in each of the following statements. (Note: There may be more than one error per statement.): (10 Points)
Computer Programming, I Fall 2021 3 a) scanf( "d", value ); ANS: scanf( "%d", &value ); b) printf( "The product of %d and %d is %d"\n, x, y ); ANS: printf( "The product of %d and %d is %d\n", x, y, x * y ); c) firstNumber + secondNumber = sumOfNumbers ANS: sumOfNumbers = firstNumber + secondNumber; d) if ( number => largest ) largest == number; ANS: if ( number >= largest ) largest = number; e) */ Program to determine the largest of three integers /* ANS: /* Program to determine the largest of three integers */ f) Scanf( "%d", anInteger ); ANS: scanf( "%d", &anInteger ); g) printf( "Remainder of %d divided by %d is\n", x, y, x % y ); ANS: printf( "Remainder of %d divided by %d is %d\n", x, y, x % y ); h) if ( x = y ); printf( %d is equal to %d\n", x, y ); ANS: if ( x == y ) printf( "%d is equal to %d\n", x, y ); i) print( "The sum is %d\n," x + y ); ANS: printf( "The sum is %d\n", x + y ); j) Printf( "The value you entered is: %d\n, &value ); ANS: printf( "The value you entered is: %d\n", value ); 4. What, if anything, prints when each of the following statements is performed? If nothing prints, then answer “Nothing.” Assume x =2 and y=3. (10 Points) a) printf( "%d", x ); ANS: 2 b) printf( "%d", x + x );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Computer Programming, I Fall 2021 4 ANS: 4 c) printf( "x=" ); ANS: x= d) printf( "x=%d", x ); ANS: x=2 e) printf( "%d = %d", x + y, y + x ); ANS: 5 = 5 f) z = x + y; ANS: Nothing. Value of x + y is assigned to z. g) scanf( "%d%d", &x, &y ); ANS: Nothing. Two integer values are read into the location of x and the location of y. h) // printf( "x + y = %d", x + y ); ANS: Nothing. This is a comment. 5. Which, if any, of the following C statements contain variables whose values are replaced? (10 Points) a. scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f) ; b. p = i + j + k + 7; c. printf( "Values are replaced") ; d. printf( "a = 5"); ANS: a and b. 6. Write a program that prints the numbers 1 to 4 on the same line. Write the program using the following methods. (15 Points) a. Using one printf statement with no conversion specifiers. ANS: printf( "1 2 3 4\n\n" ); // part a b. Using one printf statement with four conversion specifiers. ANS: printf( "%d %d %d %d\n\n", 1, 2, 3, 4 ); // part b c. Using four printf statements. ANS: printf( "1 " ); // part c printf( "2 " ); printf( "3 " );
Computer Programming, I Fall 2021 5 printf( "4\n" ); 7. What does the following code print? (5 Points) printf( "*\n**\n***\n****\n*****\n" ) ; ANS: * ** *** **** ***** 8. Write a single pseudocode statement that indicates each of the following (15 Points) : a. a) Display the message "Enter two numbers". ANS: print “Enter two numbers” b. b) Assign the sum of variables x, y, and z to variable p. ANS: p = x + y + z c. c) The following condition is to be tested in an if…else selection statement: The current value of variable m is greater than twice the current value of variable v. ANS: if m is greater than twice v do this ... else do this ... d. Obtain values for variables s, r, and t from the keyboard. ANS: input s, input r, input t 9. Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] (10 Points) a)if ( age >= 65 ); puts( "Age is greater than or equal to 65" ); else puts( "Age is less than 65" ); ANS: int x = 1, total = 0; // total not originally initialized while ( x <= 10 ) { total += x; ++x; }
Computer Programming, I Fall 2021 6 b) int x = 1, total; while ( x <= 10 ) { total += x; ++x; } ANS: int x = 1, total = 0; // total not originally initialized while ( x <= 10 ) { total += x; ++x; } c) While ( x <= 100 ) total += x; ++x; ANS: while ( x <= 100 ) { // while changed to lowercase total += x; ++x; } // } added d) while ( y > 0 ) { printf( "%d\n", y ); ++y; } ANS: while ( y > 0 ) { printf( "%d\n", y ); --y; // ++ changed to --, loop never ends otherwise } 10. Formulate a pseudocode algorithm for each of the following: (15 points) a. Obtain two numbers from the keyboard, compute their sum and display the result. ANS: Input the first number Input the second number Add the two numbers Output the sum b. Obtain two numbers from the keyboard, and determine and display which (if either) is the larger of the two numbers. ANS: Input the first number from the keyboard Input the second number from the keyboard If the first number is greater than the second number
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Computer Programming, I Fall 2021 7 Print it Else if the second number is greater than the first number Print it Else Print a message stating that the numbers are equal c. Obtain a series of positive numbers from the keyboard, and determine and display their sum. Assume that the user types the sentinel value -1 to indicate “end of data entry.” ANS: Input a value from the keyboard While the input value is not equal to -1 Add the number to the running total Input the next number Print the sum
Computer Programming, I Fall 2021 8 Assignment 1 Part2 (LAB) 1. Write a program that asks the user to enter two numbers, obtains them from the user and prints their sum, product, difference, quotient and remainder (10 Points) . #include <stdio.h> int main( void ) { int x; // define first number int y; // define second number printf( "%d", "Enter two numbers: " ); // prompt user scanf( "%d%d", &x, &y ); // read values from keyboard // output results printf( "The sum is %d\n", x + y ); printf( "The product is %d\n", x * y ); printf( "The difference is %d\n", x - y ); printf( "The quotient is %d\n", x / y ); printf( "The remainder is %d\n", x % y ); } 2. Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the lar ger number followed by the words “is larger.” If the numbers are equal, print the message “These numbers are equal.” (10 Points) int main( void ) { int x; // define first number int y; // define second number printf( "%s", "Enter two numbers: " ); // prompt scanf( "%d%d", &x, &y ); // read two integers // compare the two numbers if ( x > y ) { printf( "%d is larger\n", x ); } if ( x < y ) { printf( "%d is larger\n", y ); } if ( x == y ) {
Computer Programming, I Fall 2021 9 puts( "These numbers are equal" ); } } 3. Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the constant value 3.14159 for Π . Perform each of these calculations inside the printf statement(s) and use the conversion specifier %f. (15 Points) #include <stdio.h> int main( void ) { int radius; // circle radius printf( "%s", "Input the circle radius: " ); // prompt user scanf( "%d", &radius ); // read integer radius // calculate and output diameter, circumference and area printf( "\nThe diameter is %d\n", 2 * radius ); printf( "The circumference is %f\n", 2 * 3.14159 * radius ); printf( "The area is %f\n", 3.14159 * radius * radius ); } 4. Write a program that reads in two integers and determines and prints whether the first is a multiple of the second. [Hint: Use the remainder operator.] (15 Points) #include <stdio.h> int main( void ) { int integer1; // first integer int integer2; // second integer printf( "%s", "Input two integers: " ); // prompt user scanf( "%d%d", &integer1, &integer2 ); // read two integers // use remainder operator if ( integer1 % integer2 == 0 ) { printf( "%d is a multiple of %d\n", integer1, integer2 ); } if ( integer1 % integer2 != 0 ) { printf( "%d is not a multiple of %d\n", integer1, integer2 ); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Computer Programming, I Fall 2021 10 5. Write a program that prints the following shapes with asterisks. (10 Points) ********* *** * * * * * * *** * * * * * * ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ********* *** * * #include <stdio.h> int main( void ) { printf( "%s", "********* *** * *\n" ); printf( "%s", "* * * * *** * *\n" ); printf( "%s", "* * * * ***** * *\n" ); printf( "%s", "* * * * * * *\n" ); printf( "%s", "* * * * * * *\n" ); printf( "%s", "* * * * * * *\n" ); printf( "%s", "* * * * * * *\n" ); printf( "%s", "* * * * * * *\n" ); printf( "%s", "********* *** * *\n" ); } 6. Develop a C program that will determine whether a department store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available: (25 Points) a. Account number b. Balance at the beginning of the month c. Total of all items charged by this customer this month d. Total of all credits applied to this customer's account this month e. Allowed credit limit The program should input each fact, calculate the new balance ( = beginning balance + charges credits ) , and determine whether the new balance exceeds the customer's credit limit. For those customers whose credit limit is exceeded, the program should display the customer's account number, credit limit, new balance and the message “Credit limit exceeded.” Here is a sample input/output dialog: Enter account number (-1 to end): 100 Enter beginning balance: 5394.78 Enter total charges: 1000.00 Enter total credits: 500.00 Enter credit limit: 5500.00 Account: 100
Computer Programming, I Fall 2021 11 Credit limit: 5500.00 Balance: 5894.78 Credit Limit Exceeded. Enter account number (-1 to end): 200 Enter beginning balance: 1000.00 Enter total charges: 123.45 Enter total credits: 321.00 Enter credit limit: 1500.00 Enter account number (-1 to end): 300 Enter beginning balance: 500.00 Enter total charges: 274.73 Enter total credits: 100.00 Enter credit limit: 800.00 Enter account number (-1 to end): -1 #include <stdio.h> int main(void) { // get account number printf("%s", "\nEnter account number (-1 to end): "); int accountNumber; // current account's number scanf("%d", &accountNumber); // loop until sentinel value read from user while (accountNumber != -1) { printf("%s", "Enter beginning balance: "); float balance; // current account's starting balance scanf("%f", &balance); printf("%s", "Enter total charges: "); float charges; // current account's total charges scanf("%f", &charges); printf("%s", "Enter total credits: "); float credits; // current account's total credits scanf("%f", &credits); printf("%s", "Enter credit limit: "); float limit; // current account's credit limit scanf("%f", &limit); balance += charges - credits; // calculate balance // if balance is over limit, display account number // with credit limit and balance to two digits of precision if (balance > limit) { printf("%s%d\n%s%.2f\n%s%.2f\n%s\n", "Account: ", accountNumber, "Credit limit: ", limit, "Balance: ", balance, "Credit Limit Exceeded.");
Computer Programming, I Fall 2021 12 } // prompt for next account printf("%s", "\nEnter account number (-1 to end): "); scanf("%d", &accountNumber); } } 7. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls. Here is a sample input/output dialog: (25 Points) Enter the gallons used (-1 to end): 12.8 Enter the miles driven: 287 The miles/gallon for this tank was 22.421875 Enter the gallons used (-1 to end): 10.3 Enter the miles driven: 200 The miles/gallon for this tank was 19.417475 Enter the gallons used (-1 to end): 5 Enter the miles driven: 120 The miles/gallon for this tank was 24.000000 Enter the gallons used (-1 to end): -1 The overall average miles/gallon was 21.601423 #include <stdio.h> int main(void) { float totalGallons = 0.0; // total gallons used float totalMiles = 0.0; // total miles driven // get gallons used for first tank printf("%s", "Enter the gallons used (-1 to end): "); float gallons; // gallons used for current tank scanf("%f", &gallons); // loop until sentinel value read from user while (gallons != -1.0) { totalGallons += gallons; // add current tank gallons to total printf("%s", "Enter the miles driven: "); // get miles driven float miles; // miles driven for current tank
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Computer Programming, I Fall 2021 13 scanf("%f", &miles); totalMiles += miles; // add current tank miles to total // display miles per gallon for current tank printf("The miles / gallon for this tank was %f\n\n", miles / gallons); // get next tank's gallons printf("%s", "Enter the gallons used (-1 to end): "); scanf("%f", &gallons); } // calculate average miles per gallon over all tanks float totalAverage = totalMiles / totalGallons; printf("\nThe overall average miles / gallon was %f\n", totalAverage); }