Question

A. Write an Array Program Write a main program that counts the number of occurrences of the number 6.0 in a large array of do

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

Below is the program to do the task..

You can use sizeof() function to get the memory used by the variable in program.. and if you know the type of the variable, use sizeof() again to get the memory used by that type and divide the two values to get the size of the array..

now use simple for loop to iterate through the array and use if statement to check if the value is equal to 6.0 and count the number of occurrences. Also maintain a sum(double) variable to sum the values along the array..

finally you have the total number of elements in the array and number of occurrences of 6.0 and total sum of all the elements..

Divide the sum by total number of elements to get average..

To display the double value upto 7 point of precision use "%.7f" in printf.. not a dot before 7

You can uncomment whatever value of array you want to use and comment out the value you dont want to use for verification or for submission.

-----------------------------------------------------------------------------------------------------------------------------

#include<iostream>

using namespace std;

int main(void){

//double fArray[]={1.0,2.0000012,3.0,6.0,4.0,5.0,7.0,6.0,8.0,9.0};

#include "Lab8AdataSetX.h"

//#include "Lab8datasample.h"

int len = sizeof(fArray)/sizeof(double);

int totalSixes =0;

double sum=0;

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

if(fArray[i]==6)

totalSixes++;

sum+= fArray[i];

}

printf("array of size %d has %d occurrences of 6.0 \n",len,totalSixes);

printf("Average of the array is %.7f",sum/len);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
A. Write an Array Program Write a main program that counts the number of occurrences of...
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
  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Character Count Write a program that reads a file and counts the number of occurrences of...

    Character Count Write a program that reads a file and counts the number of occurrences of each character in the file (case sensitive). The file name is "char.txt". You should use a dictionary to hold the number of occurrences of each character, and print out that dictionary. For example, if the file contains only six characters, "tactic", your dictionary should contain the following key-value pairs (order can be different): {'a': 1, 'c': 2, 'i': 1, 't': 2} Sample Input: Follow...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • P3 - An Array of objects (120 points) Write a main program and declare an array...

    P3 - An Array of objects (120 points) Write a main program and declare an array of 5 RectangularCube objects [20pts). a. Use a loop to initialize an array of RectangularCube. In the body of the loop use the rand () function to generate random integers between 1 and 10 to assign to length, width and height data fields of each RectangularCube object (40pts) b. Write the function with header void printCube (RectangularCube rs) that receives an object of the...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

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
ADVERTISEMENT