Question

I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small...

I ONLY NEED PART 4 I HAVE DONE 1,2,3

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. into a Word document.

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.

Deliverables: Submit the following materials:

This assignment with the C programs, neatly styled and documented with comments, and meeting the

specifications as described above.

Each program will be followed immediately by the output from running the program.

MY ANSWER TO 1,2,3 JUST NEED HELP WITH 4!

1)

#include

int main(void) {

int a[10];

a[0] = 11;

a[1] = 43;

a[2] = 1;

a[3] = 34;

a[4] = 6;

a[5] = 19;

a[6] = 87;

a[7] = 99;

a[8] = 13;

a[9] = 31;

for (int i=0; i<10; i++) {

printf("%d ", a[i]);

}

return 0;

}

2)

#include

#include

#include

int main(void) {

srand(time(0));

double arr[100];

for (int i=0;i<100; i++) {

arr[i] = (double) (rand() % 100 + 1) / 2.0;

}

for (int i=0; i<100; i++) {

printf("%lf ", arr[i]);

if (i % 10 == 9) {

printf("\n");

}

}

return 0;

}

3)

#include

#include

#include

int main(void) {

srand(time(0));

int arr[100];

for (int i=0;i<100; i++) {

arr[i] = rand() % 100 + 1;

}

double avg = 0.0;

for (int i=0; i<100; i++) {

avg += arr[i];

}

avg = avg / 100.0;

printf("Average = %lf", avg);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

4)

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main(void) {

srand(time(NULL));

int count[6];

for (int i=0; i<6; i++) {

count[i] = 0;

}

for (int i=0; i<1000; i++) {

int roll = rand() % 6;

count[roll] ++;

}

for (int i=0; i<6; i++) {

printf("%d occurs %d times\n", i+1, count[i]);

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This should be done in C. I have the code written to where it gives me...

    This should be done in C. I have the code written to where it gives me the output however it is out of order from where it should be. The input should be 5 3 -7 3 5 -7 3 the output should be -7 occurs 2 times 3 occurs 3 times 5 occurs 2 times. My issue is I believe I need a bubble sort before my last loop but am unsure how to do it. #include <stdio.h> int...

  • 9. The purpose of the following program is to generate 10 random integers, store them in...

    9. The purpose of the following program is to generate 10 random integers, store them in an array, and then store in the freq frequency array, the number of occurrences of each number from 0 to 9. Can you find any errors in the program? If yes, correct them. #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 int main(void) int i, num, arr[SIZE], freq[SIZE]; srand (time (NULL)); arr[1] rand(); SIZE; for(i i 〈 i++) num arr[i]; freq[num]++; printf("InNumber occurrences...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • I have a programming assignment for a introductory c++ class and I am really struggling writing...

    I have a programming assignment for a introductory c++ class and I am really struggling writing this program. Below are the assignment requirements, I am unable to use vectors in stl and I keep getting a ton of compiling errors. Thank you chegg experts. You have really helped me out this semester. Your responses and code has cleared up so many issues ive had. Description The purpose of this challenge is to employ the use of basic functions and arrays....

  • How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...

    How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) {    string line;    ifstream myfile("input.txt");    int i = 0;    if (myfile.is_open())    {        while (getline(myfile, line))        {            arr[i];            i++;        }        myfile.close();    } } void output(int arr[]) {    ofstream myfile("output.txt");    if (myfile.is_open())    {        int i = 0;...

  • I need some with this program, please look at the changes I need closely. Its running...

    I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need: 1. All of the integers on a single line, sorted in ascending order. 2. The median value of the sorted array on a single line 3. The average of the sorted array on a single line Here is the program: #include<stdio.h> #include<stdlib.h> /* This places the Adds...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT