Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW 2 CS 1412 1. Assume everyone is familiar with searching using Google. a. Usin

ID: 3825985 • Letter: H

Question

HW 2 CS 1412

1. Assume everyone is familiar with searching using Google.
a. Using pseudocode, write a function (name, input and output only) for Google’s searching functionality.
2. On a class of computers, we know that sizeof(int) on them is 4 bytes, sizeof(short) is 2 bytes. Now write C code to define new types Int8 (1 byte integer), Int16(2 byte integer), Int32(4 byte integer)? Note that a 1 byte integer is an integer that can be represented by 1 byte. Similarly for 2 byte and 4 byte integers.
3. Consider the code
char c = '';
short s = 2;
int i = 1;
what is the value of i/s? what is the type of i/s?
what is the value of (float) i / s? (float) (i/s)?
4. Given a float variable d with value 1.5, what is the value of d - (int) d?
5. How to use printf to display the integer value of a character constant 'A'?
6. Write pseudocode for a function printInterestTable.
The input to the function is a number n (the number of years) and r (an interest rate). There is no output of this function. But the side effect of the function is to print to the screen the table of balance after each year with each interest rate for an initial deposit of $100. The first row of the table consists the following in order: years, r%, r+1%, …, r+4%. The second row consists of 1, balances after one year with corresponding rate in each column. The last row (6th) consists of 5, balances after 5 year with corresponding (compound) interest rate in each column.
For example, if N=5 and r = 6 the side effect of the function will be the following print on the screen
Years 6% 7% 8% 9% 10%
1 106.00 107.00 108.00 109.00 110.00
2 112.36 114.49 116.64 118.81 121.00
3 119.10 122.50 125.97 129.50 133.10
4 126.25 131.08 136.05 141.16 146.41
5 133.82 140.26 146.93 153.86 161.05

Your preseudocode should be a result of applying our problem solving methodologies. For format of the pseudocode, please refer to the lecture notes of L8arrays on Piazza.
Please try to use the following idea in your pseudocode.
Given an interest rate, e.g, 6%, to calculate all balances in this column, we have the following idea.
Assume we have a data item or variable balance. Initially
balance = 100.
The balance at the end of 1st year is
balance = balance * (1+0.06). Print balance.
The balance at the end of 2nd year now is
balance = balance * (1+0.06). Print balance.
……
Repeat the above until (including) 5th year.
However, the idea above works only for one interest rate. But we have 5 different interest rates to work with. We then need to introduce 5 such variables or simply an array of length 5.
7. Consider the code
void printHint(void)
{
printf(“Demo void use ”);
}
Write C-code to call/use the function above.
8. Why do we use function prototypes in a C program?
9. Consider the program
double average(int a, int b)
{
return (a + b) / 2.0;
}
what is the value of average(2.5, 5.5)?
10. Consider a program
void inializeArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
a[i] = 0

Explanation / Answer

-------------------------------------------------------------------------------------------------------------------------------------

Answer -> 7

#include<stdio.h>

void printHint(void);

int main() {

               

                printHint();

               

                return 0;

}

void printHint(void)

{

printf("Demo void use ");

}

OUTPUT->

Demo void use

----------------------------------------------------------------------------------------------------------------------------------------------

Answer -> 8

We use function prototypes in a program so that the compiler can go through the program exactly once, knowing which functions are in scope at any given time. To do this, functions would have to be declared before they're used; prototypes let you declare a function without implementing its body until later.

The function prototype tell the return type of the data that the function will return. It tells the number of arguments passed to the function. It tells the data types of the each of the passed arguments. Also it tells the order in which the arguments are passed to the function.

-----------------------------------------------------------------------------------------------------------------------------------------------

Answer ->9   

#include<stdio.h>

double average(int , int );

int main() {

               

                double d=average(2.5,5.5);

                printf("%lf",d);

                return 0;

}

double average(int a, int b)

{

return (a + b) / 2.0;

}

OUTPUT->     3.500000

--------------------------------------------------------------------------------------------------------------------------------------------

Answer – 10

#include<stdio.h>

void inializeArray(int[] , int );

int main() {

                int a[10];

                inializeArray(a,5);

                return 0;

}

void inializeArray(int a[], int n)

{

int i;

for (i = 0; i < n; i++)

{

a[i] = 0;

}

}

----------------------------------------------------------------------------------------------------------------------------------------

Answer -> 5

#include<stdio.h>

int main()

{

char cvar = 'A';

printf("Character is : %d",cvar);

return(0);

}

OUTPUT->    Character is : 65

-------------------------------------------------------------------------------------------------------------------------------------------

Answer->4

#include<stdio.h>

int main()

{

    float d=1.5;

    printf("%d",(int)d);

printf(

    return 0;

}

OUTPUT ->    1

-------------------------------------------------------------------------------------------------------------------------------------------

Answer ->3

#include<stdio.h>

int main()

{

char c = '';

short s = 2;

int i = 1;

printf("%d ",(i/s));

return 0;

}

OUTPUT-> 0