C++ File I/O
Create a program that will work for all "infile.txt" of similar structure: The first line will contain two integers. The first integer is the number of elements in array1, and the second integer the number of elements in array2. The second line will contain all the values of array1, and the third line will contain all values of array2. All numbers are separated by a space and all lines separated by a newline character.
"infile.txt" has three lines:
4 5
1 2 3 4
5 6 7 8 9
The first line has two characters 4 and 5. This the number of
elements in array1 (line 2) and array2 (line 3), respectively. The
program needs to read and store each value into a variable (say int
num1 = 4, int num2 = 5), and then create empty arrays of size 4 and
5 (ex: array1[num1], array2[num2]).
Next, the program has to read and store each integer in line 2 into
array1, and each integer in line 3 into array2.
So now we have:
int num1 = 4;
int num2 = 5;
array1[4] = [1,2,3,4];
array2[5] = [5,6,7,8,9];
Finally, the program will create an "outfile.txt", with the max number of array elements in the first line (5>4 in our case, so output 5 first), then the pairs of array1/array2 variables in each new line, as such:
"outfile.txt":
5
1 5
2 6
3 7
4 8
0 9 <- since the first array ran out of variables, use zero or another value (-1, n, etc.) as a placeholder.
Thanks
//Code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//open input file
ifstream infile("infile.txt");
if (!infile.is_open())
{
cout << "infile.txt not found";
infile.close();
return 0;
}
//size of array1
int num1;
//size of array2
int num2;
//read size of array1
infile >> num1;
//read size of array2
infile >> num2;
//max size from array1 and array2
int max = num1 > num2 ? num1 : num2;
//declaration of array1 with larger of two sizes and initialise to 0, so that when we output to outfile we just write array[index]
int array1[max] = {0};
//declaration of array2 with larger of two sizes and initialise to 0, so that when we output to outfile we just write array[index]
int array2[max] = {0};
//read array1 from input file
for (int i = 0; i < num1; i++)
infile >> array1[i];
//read array2 from input file
for (int i = 0; i < num2; i++)
infile >> array2[i];
//create or open output file
ofstream outfile("outfile.txt");
outfile << max << endl;
//output arrays to outfile
for (int i = 0; i < max; i++)
outfile << array1[i] << " " << array2[i] << endl;
//close input file
infile.close();
//close output file
outfile.close();
}
infile.txt:

outfile.txt:

C++ File I/O Create a program that will work for all "infile.txt" of similar structure: The...
(Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). The numbers are displayed in the order of their input and separated by exactly one space. (Hint: Read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.) Sample Run Enter...
c++, need help thank you
Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...
Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...
may i ask for help with this c++ problem?
this is the code i have for assignment 4 question 2:
#include<iostream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
int main()
{
string inputStr;
stack <int> numberStack;
cout<<"Enter your expression::";
getline(cin,inputStr);
int len=inputStr.length();
stringstream inputStream(inputStr);
string word;
int val,num1,num2;
while (inputStream >> word)
{
//cout << word << endl;
if(word[0] != '+'&& word[0] != '-' && word[0] !=
'*')
{
val=stoi(word);
numberStack.push(val);
// cout<<"Val:"<<val<<endl;
}
else if(word[0]=='+')
{
num1=numberStack.top();
numberStack.pop();
num2=numberStack.top();
numberStack.pop();...
What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...
Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding array elements, that array1 [0] + array2[0], etc. Save the sum into a third array called sumArray[]. Display the sum array. Submit your and the input and output of the complete execution.
Write a c++ program that does the following
Create an integer array of size 30. Assign -1 to each location in the array indicating that the array is empty. Populate half of the array with random integer values between 100 and 500 inclusive. Use the following formula in order to hash and store each number in its proper position/location. Generated Number Table Size: Should a collision occurs, use linear probing to find next available position location. Use the following probing...
This is an external javascript file // Problem 1: Declare the variables num1, num2, ans1, // greet1, greet2. Then initialize them to // hold the values 1, 2, 0, "Hello", and // "World", respectively. var num1 = 1; var num2 = 2; var ans1 = 0; var greet1 = "Hello"; var greet2 = "World"; // Problem 1a: Display the values stored in the previous // variables in the...
c++ please help Create two arrays of 20 elements, called myArray1 and myArray2 Create a function randBetween that returns an integer between 2 integer inputs (int randBetween(int min, int max)); Fill the myArray1 with the random values. Loop thru the myArray1 and find the difference between the element i and element i + 1; put that difference in array2 example: array1 = {1, 3, 4, 5} array2 ends up having {-2, -1, -1} Loop thru myArray1 and find the min...