0% found this document useful (0 votes)
23 views

112

The document contains a student's name, student ID number, and responses to various programming questions. The student provides pseudocode and a flowchart for a cashier program that validates product codes and quantities, calculates total costs, and ends when a special product code is entered. The student also answers questions about variable names, conditional operators, data type conversions, string validation, and palindrome checking.

Uploaded by

vincent 23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

112

The document contains a student's name, student ID number, and responses to various programming questions. The student provides pseudocode and a flowchart for a cashier program that validates product codes and quantities, calculates total costs, and ends when a special product code is entered. The student also answers questions about variable names, conditional operators, data type conversions, string validation, and palindrome checking.

Uploaded by

vincent 23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: Sukriansyah Laksono

NIM: 2702282982

1. Create Pseudocode and Flowchart for the following problem :


A shopping market is in need of a program to support cashier. The program will first read the
product code from screen, validate that product code should be 10 digits of characters. Other
than that, quan ty and price will be asked for each of products. Program will also need to
validate if quan ty and price is all numbers with minimum value of 1. All these process will be
repeated for every products shopped by customer.
Program will stop compu ng and display the amount that should be paid by customer when user
input product code with 0000000000.
Answer:
Pseudocode
BEGIN
Declare totalCost and assign to 0
Declare productCode
While true
Input productCode
If productCode string length != 10 con nue loop
If productCode == "0000000000"
Display totalCost
END
Input quan ty
If quan ty <= 0 con nue loop
Input price
If price <= 0 con nue loop
totalCost += (price * quan ty)
End while
END
Flow chart
2. Decide if the following statements are TRUE or FALSE
a. Variable name can be started with numbers, ex : 1name
Answer: FALSE, the first character of variable name cannot be a number or symbol
b. Variable name can not consist of more than a word
Answer: TRUE, Variable name can consist numbers
c. We can make variable with name of : system
Answer: FALSE, the variable name is a reserved C keywords
3. Do the following syntax : scanf(“%s”, temp) and gets(temp) have the same func on? What is the
output if the input is : “Good Morning” ?
Answer:

Yes it is, but it has slightly different behavior when scanning strings or array of char.
 scanf when scanning %s, as described in the documenta on

Matches a sequence of non-whitespace characters (a string).

So, one specifier variable will stop being scanned a er finding a whitespace character,
such as space, newline, carriage return, etc.
So, if we input “Good Morning” using scanf, the output will be “Good”
 gets on the other hand reads all the character from the standard input.
So, if we input “Good Morning” using gets, the output will be “Good ”
4. If all variables are integer, then state the value of A if:

B=13; C=11; D=42; E=0;

a. A = B && E;
Answer: This is an AND condi onal operator, so value other than 0 is equal to 1 or true.
So A will be false because of E = 0;
b. A = B & C;
Answer: This is an AND bitwise operator, so the value of A will be
B: 1101
C: 1011
------------------ &
A: 1001
c. A = C || D;
Answer: This is an OR condi onal operator, so value other than 0 is equal to 1 or true. So
A will be 1 or true;
d. A = B | D;
Answer: This is an OR bitwise operator, so the value of A will be
B: 0000 1101
D: 0010 1010
------------------ &
A: 0010 1111
e. A = B > 2;
Answer: This is an greater than condi onal operator, because B = 13 is greater than 2,
then A is true or 1.
f. A = B >> 2;
Answer: This is a bitshi operator, the data bits of B will be shi ed right 2 mes
B: 0000 1101
B >> 1: 0000 0110
(B >> 1) >> 1: 0000 0011
g. A = C < 3;
Answer: This is an less than condi onal operator, because C = 11 and not less than 3,
then A is false or 0.
h. A = C << 3;
Answer: This is a bitshi operator, the data bits of C will be shi ed le 3 mes
C: 0000 1011
C << 3: 0101 1000
i. A = B = C;
Answer: B will be equal to C, then A will be equal to C
j. A = B == C;
Answer: This is an equality condi onal operator, because B = 13 is and C = 11, therefore
B is not equal to C, so A will be false or 0.
5. Given the following decimal : 15870, convert it to :
a. Binary
b. Octal
c. Hexadecimal
Answer:

void toBinary(int d) {

int temp = d;

int bin[100];

int arrsize = sizeof(bin) / sizeof(int);

int i;

for (i = 0; i < arrsize; i++) bin[i] = 2;

for (i = 0; temp > 0; i++) {

bin[i] = temp % 2;

temp /= 2;

printf("binary: ");

for (i = arrsize - 1; i >= 0; i--) {

if (bin[i] != 2) printf("%d", bin[i]);

printf("\n");

}
void toOctal(int d) {

int temp = d;

int bin[100];

int arrsize = sizeof(bin) / sizeof(int);

int i;

for (i = 0; i < arrsize; i++) bin[i] = -1;

for (i = 0; temp > 0; i++) {

bin[i] = temp % 8;

temp /= 8;

printf("octal: ");

for (i = arrsize - 1; i >= 0; i--) {

if (bin[i] != -1) printf("%d", bin[i]);

printf("\n");

void toHex(int d) {

int temp = d;

int bin[100];

int arrsize = sizeof(bin) / sizeof(int);

int i;

for (i = 0; i < arrsize; i++) bin[i] = -1;

for (i = 0; temp > 0; i++) {

bin[i] = temp % 16;

temp /= 16;

printf("hex: ");

for (i = arrsize - 1; i >= 0; i--) {

if (bin[i] != -1) printf("%x", bin[i]);

printf("\n");

}
int main(int argc, char *argv[]) {

while (1) {

int input;

scanf("%d", &input);

toBinary(input);

toOctal(input);

toHex(input);

return 0;

Output

Valida on using Windows programmer calculator


6. What is the result of z = (x ==1) ? 5 : 7 if x is 0 ?
Answer: 7
7. Are these two block of codes the same?
If mark is 90, what is the output of grade from (a) and (b)?
if(mark>=85) if(mark>=85)
grade = 'A'; grade = 'A'
if(mark>=75) else if(mark>=75)
grade = 'B'; grade = 'B‘
(a) (b)

Answer:
The block (a) checks the variable mark twice with 2 different condi ons where both condi ons
can be met.
The block (b) checks the variable mark once with 2 op on condi ons where only one of the
op on condi on can be met.

So, given the mark 90, the grade for block (a) will be ‘B’, and the grade for block (b) will be ‘A’
8. What is Nested if?
Nested if is a selec on program where the selec on is performed a er the the previous
selec on’s condi on is met.
example:
If (boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}

9. What is the main difference between do-while statements and while statements in C?
Answer:
a. do-while
Execute the statements inside the DO – WHILE scope at least once, and repeat if the
condi on is met
b. while
Execute the statements inside the WHILE – DO scope while the condi on is met
10. Create the following program : N = 10

Answer:
int main(int argc, char *argv[]) {
int len = 10;
int wid = 10;
int i;
for (i = 0; i < len; i++) {
int j;
for (j = 0; j < wid; j++) {
int pos = (i * j) % (len - 1);
if (i == 0 || i == len - 1 || j == 0 || j ==
wid - 1) printf("*");
else printf(" ");
}
printf("\n");
}
return 0;
}
11. How do we validate length of a string is no more than 30 and not less than 5?
Answer:
int check_str_len(const char* str, int min_len, int max_len) {
int len = strlen(str);
return (len >= min_len && len <= max_len);
}
int main(int argc, char *argv[]) {
const char* str = "some string";
printf("%d", check_str_len(str, 5, 30));
return 0;
}
12. Create a program to convert all first le ers of every word to be uppercase, while the rest is
remain unchanged
Answer:
char input[100];

gets(input);

printf("Input: %s\n", input);

char token = input[0];

input[0] = toupper(input[0]); // Convert first char to uppercase

int i = 0;

while (token != '\0') {

if (token == ' ') input[i] = toupper(input[i]);

token = input[i];

i++;

printf("Output: %s\n", input);


13. Create a program to check if a word is palindrome!
Answer:
int isPalindrome(const char* str) {

int len = strlen(str) - 1;

if (len % 2) return 0;

char head = str[0];

char tail = str[len-1];

int i;

for (i = 0; i < len; i++) {

head = str[i];

tail = str[len-i];

if (head != tail) return 0;

return 1;

int main(int argc, char *argv[]) {

while (1) {

char input[100];

scanf("%s", input);

printf("word \"%s\" is palindrome:%d\n",

input, isPalindrome(input));

return 0;

14. Create a program to do mul plica on of these two matrices, using array 2D!
Answer:
void matrixMultiplication(void) {

int A[4][4] = {

{5, 7, 8, 2},

{3, 6, 4, 5},

{2, 7, 1, 4},

{1, 3, 9, 6}

};

int B[4][4] = {

{3, 5, 4, 8},

{2, 5, 2, 3},

{1, 1, 2, 7},

{3, 7, 6, 1},

};

int X[4][4] = {

{0, 0, 0, 0},

{0, 0, 0, 0},

{0, 0, 0, 0},

{0, 0, 0, 0},

};

int i, j, k;

for (i = 0; i < 4; i++) {

for (j = 0; j < 4; j++) {

for (k = 0; k < 4; k++) {

X[i][j] += (A[i][k] * B[k][j]);

printf("%d ", X[i][j]);

printf("\n");

You might also like