sample of code given from another question . Please follow the same code format to solve #2 . #include #inclu

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

sample of code given from another question . Please follow the same code format to solve #2 . #include #inclu

Post by answerhappygod »

sample of code given from another question . Please follow the same code format to solve #2 .
#include <iostream>#include <cstdlib>#include <fstream>using namespace std;// n and output file fsvoid starPattern(int n,ofstream &fs){ for(int i=0;i<n;i++)// loop 0 to n { for(int j=0;j<n;j++) // loop 0 to n { // if center point if(i == n/2 and j == n/2){ fs<<" *"; } // if left point else if(j < n/2 and (i == j || j == (n - i - 1) )) { fs<<" +"; // Space and Star } // if right point else if(j > n/2 and (i == j || j == (n - i - 1) ) ){ fs<<" x"; } else // otherwise { fs<<" "; } } fs<<"\n"; }}int main(int argc, char const *argv[]){ if(argc != 3){ cout<<"Not enough arguments."; exit(0); } int n; ifstream f1(argv[1]); if(!f1){ cout<<"File Name: "<<argv[1]<<" File not found"; exit(0); }
ofstream f2(argv[2]); while(f1>>n){ // call with n and f2 starPattern(n,f2); } f1.close(); f2.close(); return 0;}
Sample Of Code Given From Another Question Please Follow The Same Code Format To Solve 2 Include Iostream Inclu 1
Sample Of Code Given From Another Question Please Follow The Same Code Format To Solve 2 Include Iostream Inclu 1 (65.35 KiB) Viewed 28 times
2. Write a complete C++ program that does the following: ● You should change the programs so that they take a number from command line as an argument as the user input instead of using cin. (Validate the input if incorrect, print an error and exit), proceed to run the code printing the output to a file instead of a console. This file should be called in.txt, read the file in.txt and output the data read into another file out.txt. to take arguments when the program is executed from the command line, we must utilize these arguments in main ● argc tells us the number of arguments passed, argv tells us the arguments that were passed int main (int argc, char * argy []) It asks the user to enter an odd positive integer. The program reads a value n entered by the user. If the value is not legal, the program terminates. (Or make it repeatedly ask for a number until you get the right input) The program prints a right-angle triangle with stars with the height of n. A sample run of the program: Enter a positive odd number: 7 * * * * + ★ * * * ★ * + ★ * * * * * * * * ★★ * ★
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply