Hello, I am in assistance on seeing if I did all the following requirements righ
ID: 3854842 • Letter: H
Question
Hello, I am in assistance on seeing if I did all the following requirements right in this assignment as it is due today and I want to be absolutely sure I am following the directions right. I am not a Programming major so that will give you an idea of my experience to be lower than others, other than that here are the directions and code below:
-Write a main function that declares an array of 10 int’s. Assign each element in the array a value between 1 and 100 using 10 assignment statements – just make up the values.
-Write a for loop that will printout each of the elements in the array.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
//Initialize array of numbers of my choice between 1 and 100
int mattsArray[10] = {3, 14, 29, 36, 47, 52, 63, 78, 85, 90};
//Initialize for loop int
int m;
printf("Welcome, user! Let's take a look at Matthew Stone's number list!");
//Set up for loop to iterate 10 times through the whole array from start to finish.
for (m = 0; m < 10; m++) {
printf(" %d",mattsArray[m]);
}
return 0;
}
Part 2.
Start from scratch on each of the parts.
Write a main function that declares an array of 100 doubles.
In a for loop, assign each of the doubles a random number between 0.50 and 50.00. Here’s how.
array[i] = (double) (rand() % 100 + 1) / 2.0;
Output the elements of the array in 10 columns that are each 6 spaces wide.
Each row in the output will have 10 values. The doubles will be printed with 2 places of accuracy past the decimal.
The output of this one-dimensional array requires a single loop with an if statement inside. Even though the 100 numbers are going to be presented as a table of numbers, they are still just a list in memory.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
double oneHundred[100] = {0};
srand(time(NULL));
for(i=0;i<100; i++) {
oneHundred[i] = (double) (rand() % 100 + 1) / 2.0;
}
for(i=0;i<100;i++){
printf("%6.2lf", oneHundred[i]);
if((i+1)%10==0)
printf(" ");
}
return 0;
}
Part 3
-Write a main function that declares an array of 100 ints. Fill the array with random values between 1 and 100.
-Calculate the average of the values in the array. Output the average.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int amountRolled[1000];
int diceCount[6]={0,0,0,0,0,0};
for (i=0; i<1000;i++){
amountRolled[i] = rand()%6 + 1;
diceCount[amountRolled[i]] = diceCount[amountRolled[i]] + 1;
}
for(i=0; i<6; i++){
printf(" %d rolls %d times!",(i+1),diceCount[i]);
}
return 0;
}
Part 4
-In a main function declare an array of 1000 ints.
-Fill up the array with random numbers that represent the rolls of a die. That means values from 1 to 6.
-Write a loop that will count how many times each of the values appears in the array of 1000 die rolls.
-Use an array of 6 elements to keep track of the counts, as opposed to 6 individual variables.
-Print out how many times each value appears in the array.
1 occurs XXX times
2 occurs XXX times
Hint: If you find yourself repeating the same line of code you need to use a loop. If you declare several variables that are almost the same, maybe you need to use an array. count1, count2, count3, count4, … is wrong. Make an array and use the elements of the array as counters. Output the results using a loop with one printf statement. This gets more important when we are rolling 2 dice.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int amountRolled[1000];
int diceCount[6]={0,0,0,0,0,0};
for (i=0; i<1000;i++){
amountRolled[i] = rand()%6 + 1;
diceCount[amountRolled[i]] = diceCount[amountRolled[i]] + 1;
}
for(i=0; i<6; i++){
printf(" %d rolls %d times!",(i+1),diceCount[i]);
}
return 0;
}
Explanation / Answer
Part 1 : The code is correct but the rquirement given is "Assign each element in the array a value between 1 and 100 using 10 assignment statements " and you just assigned using 1 assignment statement. You can do the following :
#include <stdio.h>
#include <stdlib.h>
int main() {
//Initialize array of numbers of my choice between 1 and 100
int mattsArray[10] ;
mattsArray[0]=3; mattsArray[0]=14; mattsArray[0]=29; mattsArray[0]=36; mattsArray[0]=47; mattsArray[0]=52; mattsArray[0]=63; mattsArray[0]=78; mattsArray[0]=85; mattsArray[0]=90;
//Initialize for loop int
int m;
printf("Welcome, user! Let's take a look at Matthew Stone's number list!");
//Set up for loop to iterate 10 times through the whole array from start to finish.
for (m = 0; m < 10; m++) {
printf(" %d",mattsArray[m]);
}
return 0;
}
Part 2 : The given code is correct but the requirement is to put 6 spaces in between the numbers so change the printf statement :
printf("%0.2lf ", oneHundred[i]);
What printf("%6.2lf",oneHundred[i]); will do is print atleast 6 digits including the decimal point and 2 places after decimal.
Part 3 : For this case the code given is wrong. Try the following code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int array[100];
int sum=0;
float avg=0;
for (i=0; i<100;i++){
array[i] = rand()%100 + 1;
sum+=array[i];
}
avg=sum/100.0;
printf(" Average is : %f",avg);
return 0;
}
Part 4 : In the code given you are ignoring the case of 1 occuring. Try the following code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int amountRolled[1000];
int diceCount[6]={0,0,0,0,0,0};
for (i=0; i<1000;i++){
amountRolled[i] = rand()%6 ;
diceCount[amountRolled[i]] = diceCount[amountRolled[i]] + 1;
}
for(i=0; i<6; i++){
printf(" %d rolls %d times!",(i+1),diceCount[i]);
}
return 0;
}
Here rand()%6 + 1 generates the number between 1 to 6, but the array diceCount starts from 0 and ends at 5.So assume index 0 as 1 , index 1 as 2 and so on. Otherwise you have to take array of 7 and ignore index 0 and change the for loop initial value from 0 to 1. Your current code is incrementing values for 2 to 6 values of dice.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.