Study Guide-4
1. Know their meanings: source codeobject code interpreter compiler algorithm preprocessor RAM floppy flowchart pseudocode machine language loader linker execute semantics syntax higher level ALU CPU formal parameters recursive function function prototype function header header files call be value call by reference binary search linear search bubblesort insertion sort selectionsort 2 dimensional array
2.Write C++ functions or code fragments to perform the following operations unless stated otherwise.
a. write a function with function header int IDMX(int n, double A[])
which returns the index of the maximum element of an array A of n elements
int idmx(int n, double a[ ])
{
double maxval;
int maxindex=0;
maxval=a[0];
for (int i=1;i <n;i++)
if ( a[i]>maxval )
{
maxval = a[i];
maxindex=i;
}
return maxindex ;
}
- write a function with function header void dswapn(int n, double A[], double B[])
which swaps the first n elements of the array A with the first n elements with the array B.
void dswapn(int n, double a[], double b[])
{
double temp;
for (int i=0;i <n;i++)
{
temp=a[i];
a[i]=b[i];
b[i]=temp;
}
}
write a function with function header void szxpy(int n, double z, double x[ ], double y[ ])
which multiplies the array x of n elements by the scalar z and adds the result to the array y of n elements .
void szxpy(int n, double z , double x[], double y[])
{// this subroutine puts in y to y +zx
for (int i=0; i<n; i++)
y[i]= y[i]+z*x[i];
return;
}
d. Assume you sell 4 items with the prices according to the table
stock numberprice
76410.00
53230.00
46425.00
528 50.00
Assume you get orders giving stock numbers and the number of units ordered for that item. A stock number of 999 indicates the order is finished. Write a program which gives the total cost of the order.
int stocknum[4]={764,532,464,528};
float total=0.0, prices[4]={10.00,30.00,25.00,50.00};
int stock, unit;
cin >stock>unit;
while ( stock !=999)
{
for (i=0;i<4;i++)
if (stock ==stocknum[i])
total+=unit*prices[i];
cin >stock>unit;
}
cout <"total bill"<total;
e. Write a function with the header double ddot(int n, double a[], double b[])
which computes and returns the dot product of a and b given by
`a[0]*b[0] +a[1]*b[1] + a[2]*b[2]+ ……+a[n-1]*b[n-1]
f. Consider the 2000 nj tax table
If taxable income isMultiply by subtract
Overbut not over
020,000.0140
20,00050,000.017570.00
50,00070.000.0245420
70,00080,000.0351154.50
80,000150,000.055252775
150,000and over.06374042.50
Write a program that reads in income and determines the tax according to the table.
#include <iostream.h>
int main()
{
/* this program comeputes the nj tax from the 2000
tax table */
float lower[]={0,20000,50000,70000,80000,150000};
float multiplier[]={.014,.0175,.0245,.035,.05525,.0637};
float base[]={0,70,420,1154.50,2775,4042.50};
float income, tax;
int level;
cout < "\nEnter your income ";
cin > income;
if ( income>lower[5])//income in top bracket
tax=income*multiplier[5]-base[5];
else
{
level=4;
while(income<lower[level])//reduce bracket until
level--;//correct one is found
cout <"level "< level;
tax=income*multiplier[level]-base[level];
}
cout <" for income of " < income <" your tax is " <tax<endl;
return 0;
}
g Assume the digits of the decimal number 9851 are stored in an array D so that D[0]= 9, D[1]=8, D[2]=5, and D[3]=1. To obtain 9851 one can use the expression
((D[0] * 10 + D[1])*10 + D[2])*10 + D[3]
Using the concept given in the parenthesized expression above write a “for” statement which would finds the decimal equivalent of a number whose digits are stored in locations 0 through p in the D array.
Dec=D[0];
for (int I=1; I<= p;I++)
Dec=Dec*10 +D(I);
- Assume in part g. that the elements of D are 0’s and 1’s representing a binary number . Convert the binary number to a decimal
#include <iostream.h>
int main()
{
/* converts a binary number to decimal*/
int bin[]={1,1,0,1,0};
int dec,i,p;
p=4;
dec=bin[0];
for ( i=1;i<=p;i++)
dec=dec*2+bin[i];
cout< dec < endl;
return 0;
}
- write the code fragment which outputs the first n elements of an array A in reverse order starting with A[n-1].
for (int I=n-1; I>=0;I--)
cout <A(I)<endl;
- Write a program to determine the nth fibonacci number in the sequence 0 1 1 2 3 5 8…. where each number is the sum of the previous 2 numbers.
Int A[100]={0,1};
for ( int I =2;I<101)
{A[I] = A[I-1] +A[I-2];
cout <A[I]<endl;}
k.The scores on a test range from 0 to 100. Write a function void Mode(int Grades[], int n)
which accepts an array of n grades in Grades and prints which grades appear most often.
int main()
{
int freq[101]={0};
int i,max,grade;
cout < " type grades and end with 999"< endl;
cin >grade;
while (grade!=999)
{
freq[grade]++;//increase freq for grade
cin >grade;
}
max=freq[0];
for (i=0;i<=100 ;i++)//find max
if (freq[i]>max)
max=freq[i];
for (i=0;i<=100 ;i++)//print those that
if (freq[i]==max)//=max
cout < "mode is " <i< endl;
return 0;
}
/*
type grades and end with 999
24 88 67 88 24 999
mode is 24
mode is 88
*/
l.. Read in a sequence of numbers and write out the numbers without any repetitions.
int LinearSearch(int list[], int size, int key);
int main()
{
int list[1000];
int i,listlength=-1,number;
cout < " type numbers and end with 999";
cin >number;
while (number !=999)
{//find out if number is in the list
i=LinearSearch(list,listlength,number);
if (i==-1)
{
listlength++;//number is not in list
list[listlength]=number;//so add it
}
cin >number;
}
for (i=0;i<=listlength; i++)//print list
cout <list[i]<endl;
return 0;
}
int LinearSearch(int list[], int size, int key)
{
int i;
for (i = 0; i < size; i++)
{
if (list[i] == key)
return i;
}
return -1;
}
m. Repeat l. and if there are any repetitions, indicate how many times the number is repeated.
#include <iostream.h>
int LinearSearch(int list[], int size, int key);
int main()
{
int list[1000];
int freq[1000]={0};
int i,listlength=-1,number;
cout < " type numbers and end with 999";
cin >number;
while (number !=999)
{//find out if number is in the list
i=LinearSearch(list,listlength,number);
if (i==-1)
{
listlength++;//number is not in list
list[listlength]=number;//so add it
freq[listlength]=1;
}
else
freq[i]++;
cin >number;
}
for (i=0;i<=listlength; i++)//print list
cout <list[i]<" "< freq[i]<endl;
return 0;
}
int LinearSearch(int list[], int size, int key)
{
int i;
for (i = 0; i < size; i++)
{
if (list[i] == key)
return i;
}
return -1;
}
- Write a subroutine MAXNS that accepts the integer n and an array of n by2 array with the first column grades and the second social security numbers and returns the social security number corresponding to the maximum grade.
- Assume the cards in a deck are numbered between 1 and 52. You have 51 cards. Determine which one is missing.
#include <stdlib.h>
int main()
{
int card[52];
int freq[52]={0};
int i,rand1,rand2,temp;
//make deck
for ( i=0;i<52;i++)
card[i]=i;
//shuffle deck
for (int k=1;k<=100; k++)
{ rand1=rand()%52;
rand2=rand()%52;
temp=card[rand1];
card[rand1]=card[rand2];
card[rand2]=temp;
}
// find out which cards in first 51
//rather than looking at 52
for (i=0;i<=50; i++)
freq[card[i]]++;
for (i=0;i<=51; i++)
if (freq[i]==0)
cout < "missing " < i< endl;
return 0;
}
- Enter into a 2 dimensional array of characters the picture
_ _ _
1 1
1 1
1_ __ 1
nt main()
{
char A[10][10];
int i,j;
for ( i=0; i<10;i++)
for (j=0;j<10;j++)
A[i][j]=' ';
for (i=1;i<=3;i++)
{
A[0][i]='_';
A[3][i]='_';
A[i][0]='1';
A[i][4]='1';
}
for ( i=0; i<5;i++)
{
for (j=0;j<5;j++)
cout <A[i][j];
cout <endl;
}
return 0;
}
q. Write a subroutine Pascal which accepts n the order of the triangle and prints out the triangle of order n where the element in any row is the sum of the element directly above it and its diagonal left. The integer n is less than 10. Using a 2 dimensional array is very helpful. Your output may look like
1
11
12 1
1331
14641
nt main()
{
int A[10][10]={0},j;
A[0][1]=1;
/* pascal triangle*/
for (int i=1; i<5;i++)
{
for (j=1;j<=i+1;j++)
{
A[i][j]=A[i-1][j-1]+A[i-1][j];
cout < A[i][j]< " ";
}
cout < endl;
}
return 0;
}
/*
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
r. Write code that converts a decimal number to a binary number and prints it out.
int main()
{
/* converts a decimal number to binary*/
int bin[100];
int dec,i;
cout <"type a decimal number";
cin >dec;
i=0;
while (dec>0)
{ bin[i]=dec%2;
dec=dec/2;
i++; }
while (i>0)
{ i--;
cout <bin[i];
}
return 0;
}
type a decimal number26
11010
3.What do the following code segments print?
a. int A[4]= {2, 20, 53,34},A2[4]= {2,3,1,-1},j=0;
while (j!=-1)
{cout <A[j]<endl;
j=A2[j];}
b.double A[3][3]={1,2,4,3,5,7,0,6,4},x[3]=[1,0,2],b[3];
for (int I=0;I<3; I++)
{b[I]=ddot(3,A[I][0],x); //see ddot on previous page
cout <b[I]<endl;}
c. int j, A[6]={44,88,23,77,66,11}, temp;
for (int i=0; i <6; i++)
{for( j=0; j <i-1; j++)
if (A[j]< A[j+1)
{ temp = A [j];
A[j]= A[j+1];
A[j+1]= temp; }
for ( j=0; j< n; j++)
cout < A[j]<" ";
cout <endl;
}