首页 > > 详细

C language讲解、讲解computer science、C/C++编程调试、辅导c/c++程序语言 讲解R语言程序|讲解留学生Proce

Introduction to computer science – C language
Homework 4
Due Date:
20.01.2019
Save the confirmation code that
will be received from the systemSubmission Instructions :
Electronic submission is individual.
Exercise that will be submitted on time will receive 5 points bonus.
Automatic extension of 3 working days will be given, but then the 5 points bonus
won’t be given.
Appeals about wrong output won’t be accepted. You must use “diffMerge”
before submitting.
Guide Lines:
Please read the questions carefully before you start solving them.
You may assume correct input, unless otherwise specified.
Pay attention! The check will be automatic. So make sure you print in
the exact way you’re required to.
and check your output against the sample output you were given using
“diffMerge”.
o Check small/capital letters with the exercise examples and instructions.
o Going down one line after every line being printed, even if it’s the last
line.
o Don’t print spaces before and after lines.
In this exercise you may only use functions from the “stdio.h” library (not from
clrscr, delay).
Submit individually throw the course website. Submit a file name format is:
“id. zip ” (zip, not rar or anything else).
If your id is 201038833, then submit:
201038833.zip. the file will include:
o File named: “students.txt” with your name, student ID and email.
o .Solution file “hw4q1.c” for question 1
Use redirection to redirect the output to a text file.
You must submit everything with the right names.
Submission Instructions for DRY part :
Will be manual, do not submit in site. See instructions belowQuestion 1 : Functions and Pointers.
In this question we simulate a ticket reservation system for a show. When a customer
calls to reserve ticket he anticipates that all the audience for whom he reserved would
sit next to each other.
In "C-a-show" theater, there are 12 rows. In each row there are 15 seats.
Use "#define" in order to be able to convert those measures to fit other theaters.
The constant ROWS will define the number of rows in the theater.
The constant COLS will define the number of seats in each row.
You are requested to write a program that receives reservations for tickets, for each
reservation of X tickets, the program will check if it's possible to seat all the reserves
in one row, next to one another.
If it's possible, the program will save the seats for them, and will print the numbers of
the saved seats. Otherwise the program will end.
More details regarding the implementation and examples will follow after the
functions definition.
In order to implement your program, you have to implement the following functions:
1. Initializing function with the following declaration :
void InitAuditorium(int arr[ROWS][COLS])
The function receives an array and initializes it with values which indicate that the
theater is empty.
Arr – the array that contains data about the reserved seats.
2. Function of the inputs with the following declaration:
int getNumTicketsAsked()
The function gets the number of tickets to be reserved by the user and returns it.
You are allowed to assume that the user input is an integer. You have to check that the
number is a positive number (greater than zero); if the number is non-positive, then it
should return -1.
3. Calculation function with the following declaration
void findAvailableSeats(int arr[ROWS][COLS], int num, int
*row, int *col)
The function receives:
An array that contains data of the reserved seats.
The number of required adjacent seats.
A pointer to row number.
A pointer to column number.
The function searches for "num" empty adjacent seats in the same row. To do this, it
will start with [row 0, seat 0]: if it's reserved, then the function will check [row 0, seat
1] ,[row 0, seat 2],… etc. (If there are no "num" empty seats in the first row, we do the
same process for the second row, third row… etc.)
The function returns the row and the column of the first seat, of the sequence of
founded seats (by the pointers "row" and "col" respectively). Otherwise returns "-1,-
1". 4. Function with the following declaration:
void makeUnavailableSeats(int arr[ROWS][COLS], int num, int
row, int col)
The function receives:
An array that contains data of reserved seats.
The number of the reserved tickets.
A pointer to row number.
A pointer to column number.
The function marks the "num" seats as reserved, starting from the row "row" and the
column "col".
5. Calculation Function with the following declaration:
int howManyAvailableSeats(int arr[ROWS][COLS])
The function receives an array that contains data of reservation seats, and returns the
number of available seats.
Your program should use the above functions as follows:
1. First of all, initialize the array of data to indicate that the hall (theater) is empty.
(Find your way to represent that.)
2. Get more and more reservations, as long as the two following conditions apply:
- Number of reservations is positive
- There's a possibility to seat all reservation members sequently in one row.
3. At the end, [i.e., the input was illegal (non-positive number of reservation), or
there is no possibility to receive the order (There no sequence of seats of the
reservation amount)] your program should print:
Number of the available seats in the hall (with new line).
The first empty seat (row,col) in the following format:
"row=%d col=%d\n".
If there are no available seats, the program will only print "0" with no more
output. Examples:
For the following input:
15 15 11 2 15 15 15 15 15 15 15 15 3
Your output should be:
2
row=2 col=13
Explanation:
In the row 0 and 1, there will be 15 persons in each line.
In the row 2, there will be 11 persons and 2 more persons beside them.
In the following rows: 3-11, 15 persons will seat in each line. If we want to seat more
3 persons, there will be no place in the hall, due to the fact that there are just two
available seats in the row 2 (13,14).
For the following input:
15 5 4 15 15 15 2 15 15 4 15 15 15 15 15 7
Your output should be:
0
Explanation:
In the row 0 and 1, there will be 15 persons.
In the row 1, there will be 5 persons and 4 more persons beside them.
In the row 1 and 2, there will be 15 persons in each line.
Later we seated more 2 persons in row 1.
In the row 5 and 6, there will be 15 persons in each line.
Later we seated more 4 persons in row 1.
In the following rows: 3-11, 15 persons will seat in each line.
In the following rows: 7-11, 15 persons will seat in each line.
Later, when we are trying to seated more 7 persons, we find out that there is no
available seats.
Note: In the end of each line you have to add "new line" (by \n). Question 2: Dry question
For this question there is only dry submission, there is no need to submit any files to
. the course site
Bring parts 2 and 3 to the class on 20/1
Your code must be documented and readable.
Don’t forget to use indentation.
You have to submit a printed output for each section by taking a snapshot of
CodeBlock's terminal.
Definition:
Square matrix of non-negative numbers is called "diagonally dominant", if for each
row the following holds: the main diagonal's element is greater than the sum of the
elements in the same row.
In the example below, the main diagonal is marked by gray, it consists of the
elements which have the same index for row and column.
For example:
The following matrix (consist of 3 rows and 3 columns) is "diagonally dominant":
6.2 4.0 2.1
5.0 8.0 1.9
2.0 0.0 4.0
The sum of the first row, excluding the main diagonal element, is 6.1 < 6.2
The sum of the second row, excluding the main diagonal element, is 6.9 < 8.0
The sum of the third row, excluding the main diagonal element, is 2.0 < 4.0
The following matrix (consists of 5 rows and 5 columns) isn't "diagonally dominant":
8.4 4.4 1.1 1.99 0.0
5.0 9.5 1.99 0.3 0.0
2.0 0.0 4.0 2.0 0.0
0.0 0.0 1.0 0.0 0.0
1.0 1.0 0.5 1.5 4.3
The sum of the third row, excluding the main diagonal element, is 4.0 = 4.0, where
4.0 is not less 4.0. You have to implement the following function:
int diagonally_dominant(double mat[M][M])
It receives a 2-dimensional array (MxM) "mat", of non-negative numbers .
The function should check if the mat matrix is "diagonally dominant", and returns
the number of lines that don't satisfy the condition.
Regarding the above matrices: the first one the function should return 0, because
the matrix is "diagonally dominant", and for the second one the function should
return 2, because there are two lines that don’t satisfy the condition.
Complete the following code:
Question 3: Dry question
Your code must be documented and readable.
Don’t forget to use indentation.
You have to submit a printed output for each section by taking a snapshot of
CodeBlock's terminal.
#include
#define M 3
int diagonally_dominant(double mat[][M]);
int main()
{
int num, i, j;
double mat[M][M];
for (i=0; ifor (j=0; jscanf("%lf", &mat[i][j]);
}
}
num=diagonally_dominant(mat);
printf("\nWrong rows=%d\n", num);
return 0;
}Section a:
Definition: Modularity of a non-negative number x is defined as the ones digit of the
square sum of digits consisting "x".
For example:
The modularity of 1234 is 0 due to the fact that 1+4+9+16=30, and the ones
digit of 30 is 0.
The modularity of 0 is 0.
The modularity of 1, 9 and 636 is 1.
The modularity of 1111, 2, 20, 2000, 4444, 8, 53 and 4 is 4.
You have to write a program that receives a non-negative number "x", and print the
modularity of "x".
Copy and complete the following code:
#include
int main() {
int modularity;
/* TO BE COMPLETED */
printf("Please enter the number: ");
scanf("%d",&number);
/* TO BE COMPLETED */
printf("The modularity is: %d\n",modularity);
return 0;
}
Submit your code attached with the outputs of your program for the following
inputs: Number = 6875002, Number = 100670 and Number = 364690Section b:
Definition: a one-dimensional array is called "s-super-modularity" if tens digit of the
sum of the modularity of all array elements equals "s".
Example: the following array is "1-super-modularity", due to the fact that the
modularity sum of the elements is 1+2+0+5+1+3 =12, while the tens digit of 12 is 1.
1 11 0 5 1000 258
You have to write a program that receives an array of size N (N was defined in the
beginning of the program) and an integer number "s" in the range 0-9. Your program
has to print "1" in case the input array is s-super-modularity, and "0" otherwise.
Copy and complete the following code:
#include
#define N 6
int main() {
int a[N],s;
int result; /* the final result: 1 or 0 */
int i;
/* TO BE COMPLETED */
/* storing the input of the user in array a*/
printf("Please enter the array's elements : ");
for(i=0;i/* TO BE COMPLETED */
}
/* storing s */
printf("Please inter the number s: ");
scanf("%d",&s);
/* TO BE COMPLETED */
printf("The result is: %d\n",result);
return 0;
}
You can assume that the input is correct, and is in the correct range.? Submit your code attached with the outputs of your program for the
following inputs:
1. a[N] = {14,88,1,32,6875002,5} s = 2
2. a[N] = {14,88,1,32,6875002,5} s = 3
3. a[N] = {1,9,1,90,10,91} s = 1
4. a[N] = {1,9,1,90,10,91} s = 0
Good
Luck!

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!