Write it in C Arrays This assignment is designed in small progressive parts, wit
ID: 3676865 • Letter: W
Question
Write it in C
Arrays
This assignment is designed in small progressive parts, with the intention of developing the skill of working with arrays. Do each part separately. Include in your submission the code and output for Part I, then the code and output for Part 2, etc.
Specification:
Part 1.
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.
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.
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.
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.
(continued…)
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.
Explanation / Answer
Answer for Question 1:
#include<stdio.h>
int main()
{
in array[10], i =0;
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
array[5] = 60;
array[6] = 70;
array[7] = 80;
array[8] = 90;
array[9] = 58;
for( i = 0; i<10;i++)
printf("%d ", array[i]);
return 1;
}
Answer for Question 2:
#include<stdio.h>
#include<stdlib.h>
int main()
{
double array[100];
int i = 0, count = 0;
for(i = 0 ; i< 100; i++)
{
array[i] = (double) (rand() % 50.0 + 0.50) / 2.0;
}
for(i = 0; i< 100; i++)
{
printf("%.2lg ",array[i]);
}
return 1;
}
Answer for Question 3:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int array[100] ;
int i = 0;
float avg = 0.0, total = 0.0;
for(i = 0; i< 100; i++)
{
array[i] = (rand() % 100 + 1);
total += array[i];
}
avg = total/100;
printf("%f",avg);
return 1;
}
Answer for Question 4:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int array[1000], count1 = 0, count2 = 0, count3 = 0;
int i = 0, count4 = 0, count5 = 0, count6 = 0,;
for(i = 0; i<1000; i++)
array[i] = (rand() % 6 + 1);
for(i = 0; i<1000; i++)
{
if(array[i] == 1)
count1++;
else if(array[i] == 2)
count2++;
else if(array[i] == 3)
count3++;
else if(array[i] == 3)
count4++;
else if(array[i] == 3)
count5++;
else if(array[i] == 3)
count6++;
}
printf("1 Occures %d times ",count1);
printf("2 Occures %d times ",count2);
printf("3 Occures %d times ",count3);
printf("4 Occures %d times ",count4);
printf("5 Occures %d times ",count5);
printf("6 Occures %d times ",count6);
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.