Question

For C++: (Use only conditions, loops, user defined functions and arrays) Suppose you had data in...

For C++: (Use only conditions, loops, user defined functions and arrays) Suppose you had data in a file (input.txt) like this

1 2 6 11 4 8 12 3 16 5 6 14 ………

Find the multiples of 2 in order and store it in the output file (output.txt) like this

2 4 ..........

The number 6 is in the wrong position so the order was disturbed. 6 only comes after 4. Therefore 6 and the other multiples of 2 were also skipped.

Homework Answers

Answer #1

// Header Files

#include <iostream>

#include <fstream>

#include<conio.h>

#include<stdio.h>

//Standard namespace declaration

using namespace std;

//Main Function

void main () {

const int SIZE = 100;

std::ifstream fin("input.txt");

std::ofstream outfile("output.txt");

outfile.open();

if (fin.is_open())

{

std::string words[SIZE];

std::string str;

std::cout << "Read from a file!" << std::endl;

for (int i = 0; (fin >> str) && (i < SIZE); ++i) // Will read up to eof() and stop at every whitespace it hits

{

words[i] = str;

}

fin.close();

std::cout << std::endl;

int multiple=2;

for (int i = 1; i <=SIZE; ++i)

{

if( words[i] == multiple*i){

outfile << words[i];

multiple=multiple*i;

}

//std::cout << words[i] << std::endl;

}

}

else cout << "Unable to open file";

getch();

}

Know the answer?
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
Similar Questions
Please write it in c# program please and if possible no arrays only loops Loop Introduction...
Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
**C code only In this part, you will write a simple user-defined function that prints a...
**C code only In this part, you will write a simple user-defined function that prints a message. This function does not require any parameters or return value. The purpose is simply to get you started writing functions.Open repl project Lab: User-Defined Functions 1. Write a program that calls a function. This function should do the following: 1.Ask the user their name 2. Print a "hello" message that includes the user's name Example output: Please enter your name: ​Mat Hello, Mat!...
Only use C for this problem. To start, create a structure with arrays. Any structure you...
Only use C for this problem. To start, create a structure with arrays. Any structure you decide on is ok, just create your own. That said, you are required to meet these guidelines: 1. You must give the structure you make a name. Any is ok 2. Your structure must have at least 3 members. 3. Two of those members must be arrays. 4. Your members need to be of at least two different data-types. To clarify, your members cannot...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
The second assignment will use C++ syntax and control flow stuctures (e.g. loops, switch statements, functions,...
The second assignment will use C++ syntax and control flow stuctures (e.g. loops, switch statements, functions, const int variables, structs, the standard library, enums, array, etc.) to create a pattern drawing application. A user of your application will be able to select a shape to draw that will be printed out to standard output using std::cout. This application will draw a fixed size 8x8 pattern. The patterns your application will support are: filled, half-left filled, banded, checkered, a square, an...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing text files, writing functions, and program planning and development. You will read a data file and store all of the input data in a two dimensional array. You will perform calculations on the data and store the results in the 2 dimensional array. You will sort the array and print the results in a report. Instructions You will read in the same input file...
C++. Write a program that draws a rocket shape on the screen based on user input...
C++. Write a program that draws a rocket shape on the screen based on user input of three values, height, width and stages. The type of box generated (i.e.. a hollow or filled-in) is based on a check for odd or even values input by the user for the box height (or number of rows). Here is the general specification given user input for the height of the box..... Draw a hollow box for each stage if the value for...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT