+1 678 648 4277 
Table Of Contents
  • Assignment 1
  • Multiplication Table

Assignment 1

Multiplication Table

1 Write a code that asks for two integers, N and R, where R must be a factor of N.

1A.2 Write a code that prints out an N * N multiplication table in one line.
1A.3 Suppose that R stands for number of entries printed in a row. Write a code which printsout an N * N multiplication table in R columns and (N/R) rows. For example, if N = 10and R = 5, the output should be:
10 20 30 40 50
60 70 80 90 100
1B.1 Modify 1A such that R can be any positive integer that is not larger than N.
1B.2 Write a code which prints out an N * N multiplication table in R columns and (N/R)
rows. At times when R is not a factor of N, print the remaining numbers in a new row
below. For example, when N = 10 and R = 4, the output should be:
10 20 30 40
50 60 70 80
90 100 .
Produce 1 script for part 1A and 1 script for part 1B. Ignore the dot.
Coin Flipping Again
In homework 1, the task was to flip 4 coins randomly. Now, we would like to flip any number ofcoins for a certain maximum number of times.
1 Write a code which asks the user to input the number of coins that they would like to flipas well as the maximum number of repetitions.
2A.2 Write a code which flips the user-requested number of coins and prints the outcome insequence. For example, in the case of 7 coins:
Outcome: 1 0 0 1 1 0 1
2A.3 Modify your code such that it flips the coins over again for the user-requested number ofrepetitions. For example, in the case of 3 repetitions:
Outcome: 1 0 1 0 1 0 1
Outcome: 0 0 1 0 0 1 0
Outcome: 0 1 0 1 1 1 0
2A.4 Modify your code to immediately stop repeating if the outcome is all tails. Before endingthe program, display the total number of repetitions.
Produce 1 script for the entire Question 2.
Car Parking
Imagine you have a car park with 10 rows and 10 spaces available in each row. Next, assumethat vehicles entering the car park can be described as size 1, 2, 3 or 4. Vehicles are to fullyoccupy a row before starting a new one. However, when a large vehicle enters, there may not beenough space in that row to accommodate it. In such an event, skip the remaining spaces andstart the new row. End when the last row is completed.
1 Write a code which fills up a single row, ensuring that the number of spaces occupied isnot exceeded. The vehicle size should be generated using (rand() % 4) + 1. When the row isfilled up, report the number of vehicles in the row.
3A.2 Modify your code such that records the size of the vehicle that was unable to fit into theremaining spaces of a row. For example, when there are 3 spaces left but a vehicle of size 4enters the car park:
Extra vehicle = 4
No space for vehicle of size 4 in this row.
3B.1 Write a code which fills up the 10 rows with vehicles. Each row may begin from 0 spacesoccupied. Assume that vehicles that do not fit into remaining spaces are “rejected”.
3B.2 Modify your code such that the extra vehicle which does not fit into the end of the row isautomatically placed at the start of the new row. For example, take this scenario where thevehicles entering the car park were of size: 3, 4, 2, 2:
0 spaces occupied in row 1
3 spaces occupied in row 1
7 spaces occupied in row 1
9 spaces occupied in row 1
No space in row 1 for vehicle of size 2
2 spaces occupied in row 2
Produce 1 script for part 3A and 1 script for part 3B.
Expense calculator
We would like to program a simple expense calculator which gives the user an option to choosebetween a daily tally and a weekly tally.
1 Write a code which requests the user to key in a daily allowance limit, followed by
expenses. The expenses should be keyed in one by one. Recording of expenses should be
terminated by entering „0‟. Return the outcome of the sum of expenses as well as the remainingallowance, including negative values. Notify the user if expenses have exceeded allowance.
4B.1 Modify your code to begin on the first day of the week. Thus, whenever „0‟ is entered,return the outcome as per 4A.1 and ask the user if they would like to move on to the next day ofthe week. If „N‟, allow the user to “restart” the current day.
4B.2 Modify your code to begin with asking the user if they would like to use the calculator indaily mode or weekly mode. In daily mode, the calculator should just ask to do_Again after ithas completed one calculation. In weekly mode, the calculator should indicate the current day ofthe week.
int main (void)
{
int choice;
printf(“Daily or weekly? (1 / 2): “);
scanf(“%d”, &choice);
printf(“\n\n”);
if (choice == 1)
DAILY( ); // void DAILY (void), no inputs or outputs
if (choice == 2)
WEEKLY( ); // void WEEKLY (void)
return 0;
}
Produce 1 script for part 4A and 1 script for part 4B. 
Solution
1)
MultiplicationTable_1A.c
#include
#include
int main()
{
/*
* 1A.1
* Write a code that asks for two integers, N and R, where R must be a factor of N(Check).
*/
int N = 0; //declare and initialize N and R
int R = 0;
int i = 0; // variable for loop
int j = 0; // variable for loop
int m = 0;
do
{
// function ‘printf’ – output prompt
printf(“Please enter N and R, where R must be a factor of N:\n”);
printf(“N: “);
scanf_s(“%d”, &N);
printf(“R: “);
scanf_s(“%d”, &R);
}
// R must be factor of N,
// so input loop will be repeated until the remainder of the division of N by R isn’t zero
while (N % R != 0);
/*
*1A.2
*Write a code that prints out an N * N multiplication table in one line.
*/
printf(“N * N multiplication table in one line:\n”);
for (i = 1; i <= N; i++)
{
printf(“%d “, N * i);
}
printf(“\n”); // line break
/*
*1A.3
*Suppose that R stands for number of entries printed in a row.
* Write a code which prints out an N * N multiplication table
* in R columns and (N/R) rows.
*/
printf(“N * N multiplication table in R columns and (N/R) rows:\n”);
m = 1;
for (j = 1; j <= N / R; j++)
{
for (i = 1; i <= R; i++)
{
printf(“%d “, N * m);
m++;
}
printf(“\n”); // line break
}
printf(“Press any key to exit…”);
_getch(); // function ‘getch’ – wait any symbol to read
return 0;
MultiplicationTable_1B.c 
#include
#include
int main()
{
/*
* 1B.1
* Modify 1A such that R can be any positive integer that is not larger than N (Check).
*/
int N = 0; //declare and initialize N and R
int R = 0;
int i = 0; // variable for loop
do
{
// function ‘printf’ – output prompt
printf(“Please enter N and R, where R can be any positive integer that is not larger than N:\n”);
printf(“N: “);
scanf_s(“%d”, &N);
printf(“R: “);
scanf_s(“%d”, &R);
}
// R can be any positive integer that is not larger than N,
// so input loop will be repeated:
// if N <= R or
// if R is negative or zero
while (N <= R || R <= 0);
/*
*1B.2
*Write a code which prints out an N * N multiplication table in R columns and (N/R) rows.
*At times when R is not a factor of N, print the remaining numbers in a new row below.
*/
printf(“N * N multiplication table in R columns and (N/R) rows:\n”);
for (i = 1; i <= N; i++)
{
printf(“%d “, N * i);
if (i % R == 0)
{
printf(“\n”); // line break
}
}
printf(“\nPress any key to exit…”);
_getch(); // function ‘getch’ – wait any symbol to read
return 0;
2)
CoinFlipping.c
#include
#include
#include
int main()
{
int max_flipping_nums; // variable for maximum number of flipping times
int coins; // variable for the number of coins to flip
int repetititions_num; // variable for repetititions number
int i; // variable for coins loop
int coin_value; // variable for coin flip value
bool all_tails; // variable which indicates that all coins are tail in repetitition
/*
* 2A.1
* Write a code which asks the user to input the number of coins that they would like to flip
* as well as the maximum number of repetitions.
*/
printf(“Enter the maximum number of flipping times:”);
scanf_s(“%d”, &max_flipping_nums);
// asks the user to input the maximum number of repetitions
printf(“Enter the number of coins to flip:”);
scanf_s(“%d”, &coins);
repetititions_num = 1;
do
{
all_tails = true;
/*
* 2A.2
* Write a code which flips the user-requested number of coins and prints the outcome in
* sequence.
*/
for (i = 0; i < coins; i++)
{
coin_value = rand() % 2;
printf(“%d “, coin_value);
if (coin_value != 0) // if coin isn’t zero it means that the outcome isn’t all tails
{
all_tails = false;
}
}
printf(“\n”);
// 2A.4 Modify your code to immediately stop repeating if the outcome is all tails.
if (all_tails)
{
break; // stop repeating if the outcome is all tails
}
repetititions_num++;
/*
* 2A.3
* Modify your code such that it flips the coins over again for the user-requested number of
* repetitions.
*/
} while (repetititions_num <= max_flipping_nums); // repeat until repetitition limit isn’t exceeded
/*
* 2A.4
* Before ending the program, display the total number of repetitions.
*/
printf(“Number of repetitions = %d:\n”, repetititions_num);
printf(“Press any key to exit…”);
_getch(); // function ‘getch’ – wait any symbol to read
return 0;
3)
CarParking.c
 #include
#include
#include
int main()
{
int vehicle_size; // randomly generated vehicle size
int row; // current row in parking
int parked_vehicles; // parked cars in current row
int occupied_spaces; // occupied spaces in current row
row = 1;
occupied_spaces = 0;
parked_vehicles = 0;
do
{
printf(“%d spaces occupied in row %d\n”, occupied_spaces, row);
/*
* 3A.1
* The vehicle size should be generated using (rand() % 4) + 1.
*/
vehicle_size = (rand() % 4) + 1; // generate vehicle size
/*
* 3A.2
* Modify your code such that records the size of the vehicle that was unable to fit into the
* remaining spaces of a row.
*/
if (occupied_spaces + vehicle_size > 10) // if no available spaces for vehicle than go to the next row
{
printf(“No space for vehicle of size %d in row %d\n\n”, vehicle_size, row);
printf(“%d vehicles are parked in row %d\n\n”, parked_vehicles, row)
/*
* 3B.2
* Modify your code such that the extra vehicle which does not fit into the end of the row is
* automatically placed at the start of the new row.
*/
parked_vehicles = 1; // move vehicle to the next row
occupied_spaces = vehicle_size; // allocate space for this vehicle in new row
row++;
}
else
{
/*
* 3A.1
* Write a code which fills up a single row, ensuring that the number of spaces occupied is
* not exceeded.
*/
// if parking is available for vehicle
parked_vehicles++; // inc counts of vehicle in row
occupied_spaces += vehicle_size; // added vehicle size to occupied space
}
//3B.1 Write a code which fills up the 10 rows with vehicles.
} while (row <= 10);
printf(“Press any key to exit…”);
_getch(); // function ‘getch’ – wait any symbol to read
return 0;
4)
ExpenseCalculator.c 
#include
#include
void DAILY()
{
int current_expense = 0;
int expense_num = 1;
int expense_sum = 0;
int daily_allowance;
/* 4A.1
* Write a code which requests the user to key in a daily allowance limit
*/
printf(“Daily allowance limit: “);
scanf_s(“%d”, &daily_allowance);
printf(“\n\n”);
do
{
//request the user to enter next expense
printf(“Expense #%d: “, expense_num);
scanf_s(“%d”, ¤t_expense);
expense_num++;
expense_sum += current_expense; // add this expense to expenses sum
}while (current_expense != 0);
// print expenses sum
printf(“The sum of expenses: %d\n”, expense_sum);
// and remaining allowances
printf(“The remaining allowances: %d\n”, daily_allowance – expense_sum);
// if expenses sum exceeded allowance limit – print message
if (expense_sum > daily_allowance)
{
printf(“!!!Expenses have exceeded allowance!!!\n”);
}
}
void WEEKLY()
{
int choice;
int day_of_week = 1;
do
{
/*
* 4B.1
* Modify your code to begin on the first day of the week.
*/
printf(“Day: %d\n”, day_of_week);
DAILY();
// if we don’t have last day of week
if (day_of_week < 7)
{
// ask user to decide: repeat day expanses entering or move on to next day of week
printf(“Would you like to move on to the next day of the week? (1 / 0): “);
scanf_s(“%d”, &choice);
}
if (choice == 1)
{
day_of_week++;
}
} while (day_of_week <= 7); // work until days limit isn’t exceeded
}
int main()
{
/* 4B.2
* Modify your code to begin with asking the user if they would like
* to use the calculator in daily mode or weekly mode.
*/
int choice;
printf(“Daily or weekly? (1 / 2): “);
scanf_s(“%d”, &choice);
printf(“\n\n”);
if (choice == 1)
DAILY(); // void DAILY (void), no inputs or outputs
if (choice == 2)
WEEKLY(); // void WEEKLY (void)
printf(“Press any key to exit…”);
_getch(); // function ‘getch’ – wait any symbol to read
return 0;
}