Question

In: Computer Science

Write a C++program that initializes an array called resistances with the following initializer list: from file...

Write a C++program that initializes an array called resistances with the following initializer list: from file called "resistances. txt" {12.3, 98.5, 15.15, 135, 125} Using a while-loop, compute the sum of the elements of the resistances array and then obtain the average resistance and d isplay it. Then, use another loop to display the resistances that has value smaller than the average resistance that

Solutions

Expert Solution

// TEXT CODE

#include <bits/stdc++.h>
using namespace std;
  
// driver code
int main()
{
// filestream variable file
fstream file;
string word, filename;
  
// filename of the file
filename = "resistances.txt";
  
// opening file for getting number of numbers
file.open(filename.c_str());
  
int size = 0;
// getting size
while (file >> word)
{
// displaying content
size++;
}
  
// declare an array resistances od size "size"
  
double resistances[size];
  
// opening file again for extracting word
fstream file1;
file1.open(filename.c_str());
  
int i = 0;
  
// extracting words from the file and saving it into array resistances
while (file1 >> word)
{
resistances[i] = stod(word);
i++;
}
  
// get the sum
double sum = 0;
for(int i = 0; i < size; i++){
sum = sum + resistances[i];
}
  
// get average
double average = sum/size;
  
cout << "Average is: " << average << endl;
  
  
// now print all numbers lesser than average
cout << "resistances less than average resistance are: ";
for(int i = 0; i < size; i++){
if(resistances[i] < average)
cout << resistances[i] << ", ";
}
  
return 0;
}

// resistances.txt file

12.3, 98.5, 15.15, 135, 125

// OUTPUT (After running for resistances.txt file)

Average is: 77.19                                                                                                                                                 

resistances less than average resistance are: 12.3, 15.15,      


Related Solutions

In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that contain what ever arbitrary integers and print out the array including the elements and use different functions so that the main only contains variables and function calls.
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be used         - how to instantiate and initialize data members that are user-defined types at the same time
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT