You are on page 1of 16

TAREA #4

UNIVERSIDAD DE GUANAJUATO
DICIS Dem Yuriria
Programacin en Ingeniera

30/11/2015
M.I.Ana Laura Martnez Herrera
Claudia Olidia Fuentes Ortiz

1. A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges
an additional $0.50 per hour for each hour or park thereof in excess of three hours. The maximum
change for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at
a time. Write a program that calculates and prints the parking charges for each of three customers.
Your program should print the results in a neat tabular format and should calculate and print the
total of yesterdays receipts. The program should use the function CalculateCharges to determine
the charge for each customer. Your outputs should appear in the following format:
Car
1
2
3
TOTAL

Hours
1.5
4.0
24.0
29.5

Charge
2.00
2.50
10.00
14.50

#include<iostream>
#include<iomanip>
using namespace std;
float calcuteCharges(float);
int main()
{
int i;
float coche1,coche2,coche3,totalImporte,totalHoras;
cout<<"Ingrese el total de horas del choche"<<endl;
cin>>coche1;
cout<<"Ingrese el total de horas del choche"<<endl;
cin>>coche2;
cout<<"Ingrese el total de horas del choche"<<endl;
cin>>coche3;
cout<<"Car
"<<setw(10)<<"Hours"<<setw(9)<<"Charge"<<setw(5)<<endl;
cout<<" 1 "<<setw(7)<<coche1
<<setw(9)<<calcuteCharges(coche1)<<setw(5)<<endl;
cout<<" 2 "<<setw(7)<<coche2
<<setw(9)<<calcuteCharges(coche2)<<setw(5)<<endl;
cout<<" 3 "<<setw(7)<<coche3
<<setw(9)<<calcuteCharges(coche3)<<setw(5)<<endl;
totalHoras = coche1+coche2+coche3;
totalImporte =
calcuteCharges(coche1)+calcuteCharges(coche2)+calcuteCharges(coche3);
cout<<"Total"<<setw(7)<<totalHoras<<setw(9)<<totalImporte<<setw(5)
<<endl;
return 0;
}
float calcuteCharges(float hora)
{
if( hora<=3)
{
return 2.0;
}
else if (hora==24)
{
return 10.0;
}

2:
Answer each of the following questions:
A) What does it mean to choose numbers at random?
It means that you dont know what numbers you will choose.
B) Why is the rand function useful for simulating games of chance?
As I said before the player wont know the numbers that will appear so that give us I high
change to win money.
C) Why would you randomize a program by using srand? Under what circumstances is it
desirable not to randomize?
Because rand does not work as we want. Is not desirable when we want to control what to
do.
D) Why is computerized simulation of real-world situations a useful technique?
As the questions says: is a simulation that saves resources, time (sometimes) and money.

3.(Random Numbers)Write a single statement that prints a number at random from each of
following sets:
a) 2, 4, 5, 8, 10.

b) 3, 5, 7, 9, 11.

c) 6, 10, 14, 18,


22.

4.
(Exponentiation)

int num;
srand(time(NULL));
num = 2 * rand() % 10;
intcout
num;<< num;
srand(time(NULL));
num = 3+(2 * rand()) % 11;
cout << num;
int num;
srand(time(NULL));
num = 6 + (4 * rand()) % 22;
cout << num;

integerPower(int base, int exponent);


integerPower(int base, int exponent)
{
If(exponent == 1)
Return base;
Else
Return base*integerPower(base,exponent -1);
}
Write a function integerPower(base, exponent) that returns the value of base ^exponent.
5.(hypotenuse Calculations) Define a function hypotenuse that calculates the hypotenuse of a right
triangle when the other two sides are given. The function should take two double arguments and
return the hypotenuse as a double. Use this function in a program to program to determine the
hypotenuse for each of the triangles shown below.

triangle
1
2
3
6. (Multiples)
Write a function
multiple that
determinates for
a pair of integers
whether the
second is a
multiple of the
first. The
function should
take two integer
arguments and
return true is the
second is a
multiple of the
first, false
otherwise. Use
this function in a
program that
inputs a series of
pairs of integers.

Side 1
3
5
8

Side 2
4
12
15

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double calculateHypotenuse( double, double);
int main()
{
int side1,side2,side3,side4,side5,side6;
cout<<"Ingrese el lado 1 y lado 2 del triangulo 1:"<<endl;
cin>>side1;
cin>>side2;
cout<<"Ingrese el lado 1 y lado 2 del triangulo 2:"<<endl;
cin>>side3;
cin>>side4;
cout<<"Ingrese el lado 1 y lado 2 del triangulo 3:"<<endl;
cin>>side5;
cin>>side6;

cout<<"Triangle"<<setw(8)<<"Side 1"<<setw(8)<<"Side
2"<<setw(11)<<"Hypotenuse"<<endl;
cout<<setw(4)<<"1"<<setw(8)<<side1<<setw(8)<<side2<<setw(13)<<
calculateHypotenuse(side1, side2) <<endl;
cout<<setw(4)<<"2"<<setw(8)<<side3<<setw(8)<<side4<<setw(13)<<
calculateHypotenuse(side3, side4) <<endl;
cout<<setw(4)<<"3"<<setw(8)<<side5<<setw(8)<<side6<<setw(13)<<
calculateHypotenuse(side4, side6) <<endl;

return 0;
}
//calculate Hypotenuse function
double calculateHypotenuse( double side, double sides )
{
double hypotenuse;
hypotenuse = sqrt(side * side + sides* sides);
return hypotenuse;
}

# include <iostream>
using namespace std;
int multiple(int a, int b);
int multiple(int a, int b)
{
if(b % a == 0)
return 0;
else
return 1;
}
int main ()
{
int a, b;
int x, z, i;
cout<<"Cuantas veces lo desea hacer?"<<endl;
cin>>z;
for (i=0; i<=z; i++)
{
cout<<"Introduzca dos numeros para saber si el segundo es multiplo
del primero"<<endl;
cin>>a>>b;
x = multiple(a, b);
if (x==0)
cout<<"Es multiplo"<<endl;
else
cout<<"No lo es"<<endl;
return 0;
}
}

7. (Prime Numbers) An integer is said to be prime if its divisible by only 1 and itself. For example
2,3,5 and 7 are prime, but 4,6,8 and 9are not.

a) Write a function that determines whether a number is prime.


#include <iostream>
using namespace std;
void numeroPrimo(int);
int main()
{
int num;
cout << "Introduzca un numero: ";
cin >> num;
numeroPrimo(num);
return 0;
}
void numeroPrimo(int n)
{
int primo = 0;
for(int i = 2; i < n; i++)
{
if(n % i == 0)
primo++;
}
if(primo > 0)
cout << "El numero no es primo";
else
cout << "Es un numero primo";
}

b) Use this function in a program that determines and prints all the prime numbers berween 2
and 10,000. How many of these numbers do you really have to test before being sure that
youve found all the primes?
#include <iostream>
using namespace std;
int numeroPrimo(int);
int main()
C)
{
Initially,
for(int i = 1; i <= 10000; i += 2)
you
{
might
if( i == numeroPrimo(i))
think that
cout << i << endl;
n/2 is the
}
upper
return 0;
limit for
}
which
int numeroPrimo(int x)
you must
{
test to
int primo = 0;
see
whether
for(int j = 2; j < x; j++)
a number
{
is prime,
if(x % j == 0)
but you
primo++;
need
only go
}
as high
if(primo == 0)
as the
return x;
square
}
root of n.
Why?
Rewrite
the
program,
and run it both ways. Estimate the performance improvement.

#include <iostream>
#include <cmath>
using namespace std;
int numeroPrimo(int);
8. (Greatest
Common
Divisor) The
greatest common
divisor (GCD) of
two integers is
the largest
integer that
evenly divides
each of the
numbers. Write a
function gcd that
returns the
greatest common
divisor of two
integers.

9. (Quality
Points for
Numeric
Grades) Write a
function
qualityPoints that
inputs a students
average and
returns 4 if a
students average
is 90-100, 3 if

int main()
{
for(int x = 1; x <= 100000; x += 2)
{
if( x == numeroPrimo(x))
cout << x << endl;
}
return 0;
}
int numeroPrimo(int x)
{
int primo = 0;
for(int j = 2; j < sqrt(x); j++)
{
if(x % j == 0)
# include
<iostream>
primo++;
using
namespace
std;
}
if(primo == 0)
int gcd(int
a, int b);
return x;
int
gcd(int
a, int b)
}
{
int menor, i, x;
if (a<b)
menor = a;
else
menor = b;
for(i=menor-1; i>0; i--)
{
if(a % i==0 && b % i==0)
return i;
}
}
int main ()
{
int a, b;
cout<<"Sus Numeros?"<<endl;
cin>>a>>b;
cout<<gcd(a,b)<<endl;
return 0;
}

the average is 80-89, 2 if the average is 70-79, I if the average is 60-69 and 0 if the average is lower
than 60.

10. What does


the following
program do?
ADDS A+A B
TIMES I MEAN
AxB.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int qualityPoints(int);
int main()
{
int average;
cout << "Enter student's average (between 0 and 100): ";
cin >> average;

ARREGLOS
1. (Double
Array

while(average < 0 || average > 100)


{
cout << "Enter correct student's average value (between 0 and
100): ";
cin >> average;
}
cout << qualityPoints(average);
return 0;
}
int qualityPoints(int average)
{
if(average >= 90)
return 4;
else if( average >= 80)
return 3;
else if(average >= 70)
return 2;
else if(average >= 60)
return 1;
else
return 0;
}

Questions)Consider a 2-by-3 integer array t. Write a declaration for t.


a) Write a declaration for t.
Int t[2][3]
b) How many rows does t have?
2
c) How many columns does t have?
3
d) How many elements does t have?

6
e) Write the names of all the elements in row 1 of t.
t[0][0], t[0][1], t[0][2]
f) Write the names of all the elements in column 2 of of t.
t[0][1], t[1][1]
g) Write a statement that sets the element of t in the first row and second column to zero.
t[0][1]=0;
h) Write a series of statements that initialize each element of t to zero. Do not use a loop.
t[]={};
or individually
i) Write a nested for statement that initializes each element of t to zero.
for (int row = 0; row<2; row++)
{
for (int col = 0; col<3; col++)
{
t[row][col] = 0;
}
}

j) Write a statement that inputs the values for the elements of t from the keyboard
for (int row = 0; row<2; row++)
{
for (int col = 0; col<3; col++)
{
t[row][col] = 0;
}
}

k) Write a series of statements that determine and print the smallest value in array t.
int smallest = t[0][0];

for (int row = 0; row<2; row++)


{
for (int col = 0; col<3; col++)
{
if t[row][col] < smallest
{
smallest = t[row][col];
}
}

l)

Write a statement that displays the elements in row 0 of t.


cout<<t[0][0]<<" "<<t[0][1]<<" "<<t[0][2]<<endl;
m) Write a statement that total the elements in column 3 of t.
int sum = t[0][2] + t[1][2] + t[2][2];
n) Write a series of statements that prints the array t in near, tabular format. List the column
subscripts as headings across the top and list the row subscripts at the left of each row.
Extra credit - not emphasized - any approach - empty strings or use iomanip
2.(Salesperson salary Ranges)Use a one-dimensional array to solve the following problem. A
company pays its salespeople on a commission basis. The salespeople each receive $200 per week
plus 9 percent of their gross sales for week. For example, a salesperson who grosses $5000 in sales
in a week receives $200 plus 9 percent of $5000, or a total of $650. Write a program (using an array
of counters) that determines how many of the salespeople earned salaries in each of the following
ranges (Assume that each salespersons salary is truncated to an integer amount):
a) $200-299 f) $700-799
b) $300-399 g) $800-899
c) $400-499 h) $900-999
d) $500-599 i) $1000 and over
e) $600-699
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[9]={};
int datos, i, ventas;
float total;
cout<<"Cuantos datos desea introducir?:"<<endl;
cin>>datos;
for(i=1; i<=datos; i++)
{

cout<<"Introduzca el monto total de ventas del


empleado:"<<endl;
cin>>ventas;
total = 200 + (ventas * 0.09);
if(total >= 200 &&
a[0]++;
else
if(total >= 300 &&
a[1]++;
else
if(total >= 400 &&
a[2]++;
else
if(total >= 500 &&
a[3]++;
else
if(total >= 600 &&
a[4]++;
else
if(total >= 700 &&
a[5]++;
else
if(total >= 800 &&
a[6]++;
else
if(total >= 900 &&
a[7]++;
else
if(total >= 1000)
a[8]++;

total <= 299)

total <= 399)

total <= 499)

total <= 599)

total <= 699)

total <= 799)

total <= 899)

total <= 999)

}
for(i=0; i<9; i++)
{cout<<a[i]<<endl;}
return 0;
}

3.(Single Array Questions)Write single statements that perform the following one-dimensional array
operations:
a) Initialize the 10 elements of integer array counts to zero.
int Counts[10]=0;
b) Add 1 to each of the 15 elements of integer array bonus.
#define SIZE=15
int bonus[ SIZE ];
for ( m = 0; m < SIZE; ++m )
{
bonus[ m ] = m += 1;
}
c) Read 12 values for double array monthyTemperatiures from the keyboard
#define SIZE =12
int monthlyTemperatures (SIZE);
for (q = 0; q<SIZE; ++q )
{
cout<< monthlyTemperatures(q);
}
d) Print the 5 values of integer array bestscores in column format
For (m = 0, m<5, ++m){
Cout<<bestScores(m); }
4: (Find the errors) Find the error(s) in each of the following statements:
A) THERE IS NO DATA SO IT SHOW US TRASH
B) THE ARRAY HAS SPACE FOR 3 ELEMENTS BUT IS HAS 4 ELEMENTS
C) YOU CANT INPUT DATA IN THOSE ARRAYS TOGETHER
5.(Duplicate Elimination) Use a one-dimensional array to solve the following problem. Real in 20
numbers, each of which is between 10 a 100, inclusive. As each number is read, validate it and store
it in the array only if isnt a duplicate of a number already read. After reading all the values, display
only the unique values that the user antlered. Provide for the worst case in which all 20 numbers
are different. Use the smallest possible array to solve this problem.

# include <iostream>
using namespace std;
int main(void)
{
const int SIZE = 20; // size of array
int a[SIZE] = { 0 };
int subscript = 0;
int duplicate;
int value; // number entered by user
cout << "Enter 20 numbers between 10 and 100:\n";
// get 20 nonduplicate numbers in the range between 10 and 100
for ( int i = 0; i < SIZE;)
{
duplicate = 0;
cin >> value;
// validate input and test if tehre is a duplicate
if ( value >= 10 && value <= 100 )
{
for ( int j = 0; j < subscript; j++ )
{
if ( value == a[ j ] )
{
duplicate = 1;
break;
} // end if
} // end for
// if number is not a duplicate enter it in array
if ( !duplicate )
{
a[subscript++ ] = value;
i++;
} // end if
else
cout << "Duplicate number.\n";
} // end if
else
cout << "Invalid number.\n";
} // end for
cout << "\nthe nonduplicate values are:\n";
// display array of nonduplicates
for ( int i = 0; 1 < SIZE; i++ )
cout << a[ i ] << ", ";

cout << endl;


return 0;

7.(What Does This Code Do?)What does the following program do?

#include<iostream>
using namespace std;
int whatIsThis(int [], int);
int main ()
{
const int arraysize=10;
int a[arraysize]={1,2,3,4,5,6,7,8,9,10};
int result =whatIsThis(a, arraysize);
cout<<"Result is"<<" "<<result<<endl;
}
int whatIsThis(int b[], int size)
{
if(size== 1)//base case
return b[0];
else//recursive step
return b[size -1]+ whatIsThis(b, size -1);
}

9. .
(What
Does
This
Code
Do?)What
does the
following
program do?

#include<iostream>
using namespace std;
void someFunction(int [], int, int);//function protype
int main ()
{
const int arraysize=10;
int a[arraysize]={1,2,3,4,5,6,7,8,9,10};
cout<<"The values in the array are:"<<endl;
someFunction(a, 0, arraysize);
cout<<endl;
}
void someFunction(int b[], int current, int size)
{
if(current < size)//base case
{
someFunction(b, current + 1, size);
cout<<b[current]<<" ";
}
}

You might also like