Question

Write a C++ program that will create an array with a given number of elements. Use size = 10 for demonstration purposes, but

1 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int arr[10], n, i, max, min;
  6.     cout << "Enter the size of the array : ";
  7.     cin >> n;
  8.     cout << "Enter the elements of the array : ";
  9.     for (i = 0; i < n; i++)
  10.         cin >> arr[i];
  11.     max = arr[0];
  12.     for (i = 0; i < n; i++)
  13.     {
  14.         if (max < arr[i])
  15.             max = arr[i];
  16.     }
  17.     min = arr[0];
  18.     for (i = 0; i < n; i++)
  19.     {
  20.         if (min > arr[i])
  21.             min = arr[i];
  22.     }
  23.   
    for(i=0;i<n;i++)
            {               
                    for(j=i+1;j<n;j++)
                    {
                            if(arr[i]<arr[j])
                            {
                                    temp  =arr[i];
                                    arr[i]=arr[j];
                                    arr[j]=temp;
                            }
                    }
            }
            
            //print sorted array elements
            cout<<"Sorted (Descending Order) Array elements:"<<endl;
            for(i=0;i<n;i++)
                    cout<<arr[i]<<"\t";
            cout<<endl;       
  24.     cout << "Largest element : " << max;
  25.     cout << "Smallest element : " << min;
  26.     return 0;
  27. }

For standard deviation,mean and median , I am writing separatle as it will be difficult to trac everything in one program

1)Standard devaition

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. float calculateSD(float data[]);
  5. int main()
  6. {
  7. int i;
  8. float data[10];
  9. cout << "Enter 10 elements: ";
  10. for(i = 0; i < 10; ++i)
  11. cin >> data[i];
  12. cout << endl << "Standard Deviation = " << calculateSD(data);
  13. return 0;
  14. }
  15. float calculateSD(float data[])
  16. {
  17. float sum = 0.0, mean, standardDeviation = 0.0;
  18. int i;
  19. for(i = 0; i < 10; ++i)
  20. {
  21. sum += data[i];
  22. }
  23. mean = sum/10;
  24. for(i = 0; i < 10; ++i)
  25. standardDeviation += pow(data[i] - mean, 2);
  26. return sqrt(standardDeviation / 10);
  27. }

2)Mean and Meridian:

#include <bits/stdc++.h>

using namespace std;

  

// Function for calculating mean

double findMean(int a[], int n)

{

    int sum = 0;

    for (int i = 0; i < n; i++)

        sum += a[i];

      

    return (double)sum/(double)n;

}

  

// Function for calculating median

double findMedian(int a[], int n)

{

    // First we sort the array

    sort(a, a+n);

  

    // check for even case

    if (n % 2 != 0)

       return (double)a[n/2];

      

    return (double)(a[(n-1)/2] + a[n/2])/2.0;

}

  

// Driver program

int main()

{

    int a[] = { 1, 3, 4, 2, 7, 5, 8, 6 };

    int n = sizeof(a)/sizeof(a[0]);

    cout << "Mean = " << findMean(a, n) << endl;

    cout << "Median = " << findMedian(a, n) << endl;

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will create an array with a given number of elements. Use...
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 to : Create array arr1 of size 10 Assign values to elements...

    Write a C++ program to : Create array arr1 of size 10 Assign values to elements such that - say index = i, arr1[i] = 10 - i. Print arr1 Create arr2 and copy alternate elements from arr1 starting from index 1. Print arr2. Sort arr1 in ascending array and print its elements from indices 3 to 8 Print every output on a different line and separate array elements by spaces.

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • 7eatng that function Write a C++ program that does the following: Fill an array of 123...

    7eatng that function Write a C++ program that does the following: Fill an array of 123 elements using srand) and rand) with random numbers between 150 and 667. Fill an array of 123 elements with random numbers between 150 and 667. Using a loop. Fill an array of 123 elements with random numbers between 150 and 667. Using a for loop Use a void function to fill an array of 123 elements with random numbers between 150 and 667, Possible...

  • C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that...

    C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that creates an array of 10 integers. The program should then use the following functions: getData() Used to ask the user for the numbers and store them into an array displayData() Used to display the data in the array displayLargest() Used to find and display the largest number in the array (prior to sort). displaySmallest() Used to find and display the smallest number in the...

  • Write a C program that uses the random number generator rand( ) to create an array...

    Write a C program that uses the random number generator rand( ) to create an array with 20 numbers with value in the range from 1 to 100. The program calculates and displays the difference between the largest array element and the second largest array element in the array.

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the...

    In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the user to input a size of an array. Create an array of type int. Ask a user to fill the array. Create a functions sortArray() and mostFrequency(). The sortArray() function will sort number into incrementing order. The sorting function must be implemented from scratch and it must not use any function from the library. Feel free to use any sorting algorithm. The program will...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

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