Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Homework: 1.A -
1.A Source Code:
//Homework 1A: Problem #3.18
//Prewriting Steps:
//Calculate salary
//Print salary
//Repeat loop until condition fails
//Drafting Steps:
//1.Declare variables
//2.Prompt user to enter sales
//3.Use "While" statement to calculate salary
//While sales is not equal to -1. System will continue to accept input until -1 is entered.
//4.Input sales
//5.Output to screen
printf("Enter Sales in dollars (-1 to end): "); //Output to screen prompts user to enter sales. -1 will end program//
scanf("%f", &sales); //Scanner reads sales to verify validity of condition//
while (sales !=-1) //While loop is executed until the condition becomes false, in this case entering -1//
{
salary = 200 + ((sales*9)/100); //Salary calculation based on input above including the extra 9%//
printf("\nSalary is $%.2f", salary); //Salary is displayed up to 2 decimal places//
printf("\nEnter Sales in dollars (-1 to end): "); //While loop executed until user enters -1//
scanf("%f", &sales); //Scanner reads sales to verify the validity of condition. Loop ends if condition is -1//
}
1A Program Output:
“Dancarlix Perez” Homework “1” Page 2 of 7
Homework: 1.B -
1.B Source Code:
//Homework 1B: Problem #3.19
//Interest Calculator
//Prewriting Steps:
//Calculate interest
//Print simple interest for loan entered
//Repeat loop until condition fails
//Drafting Steps:
//1.Declare variables
//2.Prompt user to enter values for variables
//3.Use "While" statement to calculate interest rate
//While loop runs and applies values. System will continue to accept values until loop is finished by -1 at the start of a loop.
//4.Determine the operation to calculate the interest rate
//Use given formula
//5.Output to screen
printf("Enter loan principal (-1 to end): "); //Output to screen prompts user to enter loan principal. -1 will end program//
//Note: At this stage program will run if value entered is not -1//
while (principal !=-1) //While loop is executed. At this stage entering -1 will not make the statement false, program will
continue to run//
{
printf( "\nEnter interest rate: "); //Output to screen prompts user to enter interest rate//
scanf( "%f", &interestRate); //Scanner reads input for interest rate//
printf( "\nEnter term of the loan in days: "); //Output to screen prompts user to enter the number of days for the loan//
scanf( "%d", &days); //Scanner reads input for number of days//
interest = (principal * interestRate * days)/365; //Interest calculation based on the values entered for the variables above//
printf("\nThe interest charge is $%.2f",interest); //Interest charge is displayed up to 2 decimal places//
printf("\nEnter loan principal(-1 to end): "); //While loop executed until user enters -1//
scanf("%f", &principal); //Scanner reads sales to verify the validity of condition. Loop ends if condition is -1//
}
Homework: 1.C -
1.C Source Code:
//Homework 1C: Problem #3.33
//Prewriting Steps:
//Determine square size
//Code pattern. While and nest while loop
//Repeat loop until condition fails
//Drafting Steps:
//1.Declare variables
//2.Prompt user to enter the length of each side of the square
//3.Use "While" statements to build each side until square is formed
//While loop runs and applies values.
//4.Repeat process 3 times within the main function to print 3 different size squares
//5.Output to screen
printf("\nEnter the side of the square: \n"); //Output to screen prompts user to enter loan principal. -1 will end
program//
if (row == 1)
{
printf("*");
}
else if (row == side)
{
printf("*");
“Dancarlix Perez” Homework “1” Page 5 of 7
}
else if (col == 1)
{
printf("*");
}
else if (col == side)
{
printf("*");
}
else
printf(" ");
col--;
} //By this input, asterisks will print to build the 1st row, last row, and sides
printf("\n");
row--;
col = side;
}
printf("\nEnter the side of the square: \n"); //Output to screen prompts user to enter loan principal. -1 will end
program//
if (row == 1)
{
printf("*");
}
else if (row == side)
{
printf("*");
}
else if (col == 1)
{
printf("*");
}
else if (col == side)
{
“Dancarlix Perez” Homework “1” Page 6 of 7
printf("*");
}
else
printf(" ");
col--;
}
printf("\n");
row--;
col = side;
}
printf("\nEnter the side of the square: \n"); //Output to screen prompts user to enter loan principal. -1 will end
program//
if (row == 1)
{
printf("*");
}
else if (row == side)
{
printf("*");
}
else if (col == 1)
{
printf("*");
}
else if (col == side)
{
printf("*");
}
else
printf(" ");
col--;
}
“Dancarlix Perez” Homework “1” Page 7 of 7
printf("\n");
row--;
col = side;
}