| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11241 | Simple Addition |
|
| 10842 | Equivalent relation (Exchange) |
|
Description
Four arrays A, B, C and D with size MxN. B, C, D are stored in an array of pointers, and A is stored in a separated array. The task is to add the designated elements of A and the corresponding element from a chosen array of B, C, D, and output the result.
Take sample input as an example. Size of the arrays are 3 rows by 2 columns, initial values are as the form below. Input number “2” in the sixth line indicates D is chosen array and we want the sum of elements with two indices (1,0), (2,0)
Therefore, the answer is A(1,0)+D(1,0)+A(2,0)+D(2,0)=2+8+4+10=24
You will be provided with the following sample code, and asked to implement function "addition".
#include <stdio.h>
int addition(int*, int, int*[], int*, int);
int main(void) {
int a[50][50], b[50][50], c[50][50], d[50][50];
int index_to_add[20];
int *entry[3];
int i, j, m, n, array_num, num_ind;
scanf("%d %d", &m, &n);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &b[i][j]);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &c[i][j]);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &d[i][j]);
scanf("%d", &array_num);
scanf("%d", &num_ind);
for(i=0; i<num_ind*2; i=i+2)
scanf("%d %d", &index_to_add[i], &index_to_add[i+1]);
entry[0] = &b[0][0];
entry[1] = &c[0][0];
entry[2] = &d[0][0];
printf("%d\n", addition(&a[0][0], array_num, entry, index_to_add, num_ind));
return 0;
}
int addition(int* ptr_a, int array_num, int* entry[], int* index_to_add, int num_ind){
/*your code*/
}
Input
The first line is the size of arrays, M rows by N columns. The following four lines are initial values arranged by row major. The sixth line tells B, C or D is chose. The next line is the number of elements k to be summed. And the last k lines are the row and column indices of elements to be add. 0 < M, N < 50. 0 < k < 10. Index starts from 0.
Output
An integer, sum of desired elements.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
There are N integer pointers, indexed from 0 to N-1 (N<100). Each pointer initially points to an integer of value 0.
There are two kinds of instructions.
The instruction “S n k” is used to set the integer, which pointer n points to, to be k. For example, Set 1 10 means that the integer that pointer 1 points to is set to 10.
And the instruction “E n m” means that pointer n and pointer m exchange the positions that they point to.
For example, pointer 1 points to 5 and pointer 2 points to 7. “E 1 2” means that pointer 1 points to pointer 2 points to and at the same time pointer 2 points to the pointer 1 points to. After “E 1 2”, pointer 1 points to 7 and pointer 2 points to 5.
Note that you don't have to change all the pointers if one pointer changes its target. The following table is an example.
|
instruction |
Description |
|
S 1 806 |
Pointer 1 points to the integer is set to 806
|
|
E 1 2 |
Pointer 1 points to the integer that pointer 2 points to, and at the same tome pointer 2 points to the integer that pointer 1 points to.
And you don’t have to change the target of pointer 1 and pointer 2. |
Finally, output all the values that pointers 0 to N-1 point to in order.
Note that
1. This problem involves three files.
- function.h: Function definition of execInstruct.
- function.c: Function describe of execInstruct.
- main.c: A driver program to test your implementation.
You will be provided with main.c and function.h, and asked to implement function.c.
2. For OJ submission:
Step 1. Submit only your function.c into the submission block. (Please choose c compiler)
Step 2. Check the results and debug your program if necessary.
main.c
#include <stdio.h>
#include "function.h"
#define SIZE 100
int main() {
int *ptrArr[SIZE];
int dataArr[SIZE] = {0};
char inst;
int dataNum, instNum;
int param1, param2;
int i;
/* input */
scanf("%d %d", &dataNum, &instNum);
/* initialize the ptrArr */
for (i = 0; i < dataNum; i++)
ptrArr[i] = &dataArr[i];
for (i = 0; i < instNum; i++) {
scanf(" %c %d %d", &inst, ¶m1, ¶m2);
execInst(ptrArr, inst, param1, param2);
}
/* output */
for (i = 0; i < dataNum - 1; i++) {
printf("%d ", *ptrArr[i]);
}
printf("%d", *ptrArr[i]);
return 0;
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
void execInst(int *ptrArr[], char inst, int param1, int param2);
#endif
Input
The first line contains two positive X and Y. X indicates the number of parameters. Y indicates that there are Y instructions needed to be done.
The next Y lines contain the instructions.
Output
All the values that pointers 0 to pointer N-1 point to in order. Each value is seperated by a blank ' '.
# Note that there is no '\n' at the end of the output.

