1) Write a program the contains an array of 1000 elements named rolls. The rolls
ID: 3556273 • Letter: 1
Question
1) Write a program the contains an array of 1000 elements named rolls. The rolls array should be populated using a for loop and assigned a random number simulating the roll of a pair of six sided dice. Using a second loop, your program should find the # of times the value or 7 or 11 was rolled. You program should display the probability that a 7 or an 11 was rolled.
2) Write a program that stores the following numbers in the array named miles: 15, 22, 16, 18, 27, 23, and 20 that lives in the main() function. Have your program pass the entire contents of the miles array to a function named named displayInts(). The displayInts() function should display the values of the array it received. The function should separate the printed values with a "-".
3) Write function named scalarProduct() that accepts two double arrays and returns the scalar product. A scalar product is a0b0 + a1b1 + a2b2+ ... + an-1bn-1.
Write a simple main() function to test your function.
4) Write a function named equals() that accepts two char arrays and return true if both arrays have the same characters in the same order. The function should return false otherwise. Write a simple main() function to test your function.
Explanation / Answer
1) Write a program the contains an array of 1000 elements named rolls.
The rolls array should be populated using a for loop and assigned a random number simulating the roll of a pair of six sided dice.
Using a second loop, your program should find the # of times the value or 7 or 11 was rolled.
You program should display the probability that a 7 or an 11 was rolled.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
srand(time(NULL));
int array[1000];
for(int i=0; i<1000; i++)
array[i] = (rand()%6+1) + (rand()%6+1) ;
int Seven_or_11 = 0;
for(int i=0; i<1000; i++)
if(array[i]==7 || array[i] == 11) Seven_or_11++;
cout << "probability that a 7 or an 11 was rolled." << (static_cast<double>(Seven_or_11))/1000.0 << endl;
return 0;
}
2) Write a program that stores the following numbers in the array named miles: 15, 22, 16, 18, 27, 23, and 20 that lives in the main() function.
Have your program pass the entire contents of the miles array to a function named named displayInts().
The displayInts() function should display the values of the array it received. The function should separate the printed values with a "-".
#include<iostream>
using namespace std;
void displayInts(int miles[],int size)
{
for(int i=0; i<size; i++)
{
if(i!=size-1)
cout << miles[i] << "-";
else
cout << miles[i];
}
}
int main()
{
int miles[] = {15,22,16,18,27,23,20};
displayInts(miles,7);
return 0;
}
3) Write function named scalarProduct() that accepts two double arrays and returns the scalar product. A scalar product is a0b0 + a1b1 + a2b2+ ... + an-1bn-1.
Write a simple main() function to test your function.
#include<iostream>
using namespace std;
double scalarProduct(double array1[],double array2[],int size)
{
double sum = 0;
for(int i=0; i<size; i++)
sum = sum + (array1[i]*array2[i]);
return sum;
}
int main()
{
double array1[] = {1,2,3};
double array2[] = {3,2,1};
cout <<"Scalar Product Given by " << scalarProduct(array1,array2,3) << endl;
return 0;
}
4) Write a function named equals() that accepts two char arrays and return true if both arrays have the same characters in the same order.
The function should return false otherwise. Write a simple main() function to test your function.
#include<iostream>
#include<cstring>
using namespace std;
bool equals(char array1[],char array2[])
{
if(strlen(array1)!=strlen(array2)) return false;
for(int i=0; i<strlen(array1); i++)
{
if(array1[i]!=array2[i]) return false;
}
return true;
}
int main()
{
cout << equals("Two","Two") << endl;
cout << equals("Two","TWo") << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.