Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Erlein Nicole B.

Ureta
BSCS 1A
1.) Give the output of the program and show the process on how the output
values were derived (conversion process and shifts).

#include int main() {


int a=30, b=40;
printf("The value of a<<3 is : %d . ", a<<3);
printf("\nThe value of b>>4 is : %d . ", b>>4);
return 0;
}

Step A: Convert A and B to Binary

30 = 11110
40 = 101000

Step B: Perform the operation

30 << 3 = 11110000 = 240


//shift binary digits to the left three times. Simply add 3 zeros to the left

40 >> 4 = 000010 = 2
// to right shift digits, add zero prefixes and eliminate bits from the right side each time you
do so.

Output:

2.) Create a program to compute sum of array elements of three (3) matrices
(2x2x2). Program must accept input (elements) and displays the sum.

#include<stdio.h>

int main(){
int array1[2][2][2], array2[2][2][2], array3[2][2][2];
int sumarray[2][2][2];

puts("Enter elements into the first array");


for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("\nEnter element %d %d %d: ", i + 1, j + 1, k +
1);
scanf("%d", &array1[i][j][k]);
}
Erlein Nicole B. Ureta
BSCS 1A
}
}
//
puts("\nThe elements in the 1st array are: ");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("%d ", array1[i][j][k]);
}
printf("\n");
}
printf("\n");
}
//
puts("Enter elements into the second array");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("\nEnter element %d %d %d: ", i + 1, j + 1, k +
1);
scanf("%d", &array2[i][j][k]);
}
}
}
//
puts("\nThe elements in the 2nd array are: ");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("%d ", array1[i][j][k]);
}
printf("\n");
}
printf("\n");
}
//
puts("Enter elements into the last array");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("\nEnter element %d %d %d: ", i + 1, j + 1, k +
1);
scanf("%d", &array3[i][j][k]);
Erlein Nicole B. Ureta
BSCS 1A
}
}
}
puts("\nThe elements in the 3rd array are: ");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("%d ", array3[i][j][k]);
}
printf("\n");
}
printf("\n");
}

for(int i = 0; i < 2; i++){


for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
sumarray[i][j][k] = array1[i][j][k] + array2[i][j][k] +
array3[i][j][k];
}
}
}

puts("\nThe sum of the arrays is: ");


for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("%d ", sumarray[i][j][k]);
}
printf("\n");
}
printf("\n");
}

return 0;
}
Erlein Nicole B. Ureta
BSCS 1A
Output:

3.) Type the source code in your editor. Try out the program a few times to
ensure that it accepts only hello as the proper password. Next, eliminate the
match variable from your code and use the strcmp() function directly in the if
comparison. That is how most programmers do it. Then, replace the strcmp()
function with strcasecmp(). Run the program to confirm that both hello and
HELLO are accepted as the password.

#include
int main() {
char password[]="hello";
char input[15];
int match;
printf("Password: ");
scanf("%s",input);
match=strcmp(input,password);
if(match==0){
puts("Password accepted.");
Erlein Nicole B. Ureta
BSCS 1A
}
else {
puts("Invalid password!"); return(0);
}

STEP 1: Ensure Program only accepts “hello”

STEP 2: Eliminate the match variable from your code and use the strcmp() function directly
in the if comparison.

#include<stdio.h>
#include <string.h>

int main(){
char password[] = {"hello"};
char input[15];

printf("Password: ");
scanf("%s", input);

if(strcmp(input,password) == 0){
puts("Password accepted.");
}
else {
puts("Invalid password!");
}

return 0;
}

STEP 3: Replace the strcmp() function with strcasecmp()

#include<stdio.h>
#include <string.h>

int main(){
char password[] = {"hello"};
char input[15];
Erlein Nicole B. Ureta
BSCS 1A

printf("Password: ");
scanf("%s", input);

if(strcasecmp(input,password) == 0){
puts("Password accepted.");
}
else {
puts("Invalid password!");
}

return 0;
}

Output:

4.) Write a C programming to check an array of numbers and outputs the


maximum (largest) and minimum (smallest) of the values using function.

#include<stdio.h>
#include<conio.h>

void testdata(int array[], int size);

int main(){
int size;
int array[10];
printf("Enter number of value in the array: ");
scanf("%d", &size);

puts("Enter elements in the array: ");


for(int i = 0; i < size; i++){
printf("Element no.%d: ", i+1);
scanf("%d", &array[i]);
}

testdata(array, size);
}

void testdata(int array[], int size){


Erlein Nicole B. Ureta
BSCS 1A
int big, small;

big = array[0];
for(int j = 1; j < size; j++){
if(big < array[j]){
big = array[j];
}
}

printf("The maximum value is %d", big);

small = array[0];
for(int i = 1; i < size; i++){
if(small > array[i]){
small = array[i];
}
}
printf("\nThe minimum value is %d", small);

Output:

You might also like