Question

Student ID: 123Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is

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

#include <bits/stdc++.h>
using namespace std;

const double e = 2.71828;

// Part a
// This function get's an integer N and doubel x
// It calculates values for all i -> [0,N]
// according to given formula, and returns sum.
double function_123(int N, double x) {
double sum = 0;
for (int i = 0; i <= N; i++) {
if (i == 0 || i == 1)
sum += x * pow(e, sqrt(i));
else
sum += (2.0 * sqrt(i / 2.0) + i * x) / (pow(i / 3.0, 2) + x * x);
}
return sum;
}

int main() {

// Part b.I
//This cycle asks user to enter N
//In case of negative number error is written
//and user is asked for input again
int N = 0;
while (true) {
cout << "Enter N: ";
cin >> N;
if (N > 0)
break;
cout << "Invalid input. Enter positive number.\n";
}

// Part b.II
// Answer for user's N and x = 2.0 is printed on console
cout << fixed << setprecision(5) << "Result: " << function_123(N, 2.0)
<< endl;

// Partn b.III
// Answer for N -> [1, 10] is written to sum.txt file
ofstream writer;
writer.open("sum.txt");
for (int i = 1; i <= 10; i++)
writer << fixed << setprecision(5) << function_123(i, 2.0) << endl;
writer.close();

return 0;
}

OUTPUT

Input (stdin) -5 Your Output (stdout) Enter N: Invalid input. Enter positive number Enter N: Invalid input. Enter positive nu

ntr File Edit Format View Help 7.43656 8.78656 10.47646 12.35061 14.29258 16.22560 18.10413 19.90413 21.61510 23.23458

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...
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
  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Consider making a function (mini program to complete a specific task) for each of the scenarios...

    Consider making a function (mini program to complete a specific task) for each of the scenarios below. State what the purpose of the function is in your own words, specify input that is needed by the function, what output is expected from the functions, and the step by step process that will obtain the output from the input (the algorithm). In addition to these 4 items also specify test data that can be used for each problem. Remember to describe...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and studentId as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF() function...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a working C program that will do the following: 1.Include your name, Lab Test number,...

    Write a working C program that will do the following: 1.Include your name, Lab Test number, and studentId as comments in the first lines of theprogram 2.User is first prompted to enter an integer number between 1 and 20, to be stored asvariable named number. If the number is outside the range 1-20, the program will end. 3.If the number is within the allowed range, variable number is passed to function multF().The multF() function computes and returns the result of:...

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