Please fix this program using C++, I\'m tryin to write a program that read data
ID: 3742567 • Letter: P
Question
Please fix this program using C++, I'm tryin to write a program that read data from input file (data1.txt), store them as 2 different arrays, and print out each element of arrays.
Data1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
100 80 60 40 20
Here is my code
Thanks
Quick Launch (Ctrl+Q) Programi (Debugging)-Microsoft Visual Studio Sign in File Edit View Project Build Debug Team Iools Test R Tools Analyze Window Help -Debug E | Lifecycle Events , Thread: [976] Main Thread Process: [7892] Program1.exe Header.cpp Header.h Source.cpp main0 Program 1 - (Global Scope) 2 3 4 #include #include > temp) arr [i++] temp ; = size = 1; for (i- e; iExplanation / Answer
You have initialized arrays namely numbers and numbers2 to be empty arrays with SIZE1(line 29) and SIZE2(line 36) respectively.
When you create an empty array of integers in C++ , all the values in the array are initialized to 0.
And then you are traversing through this empty array using for loop and printing the contents of these two arrays. So, it will print 0 ten times for numbers array and will print 0 five times for numbers2 array.
If you want the numbers read from the input file to be stored into these two arrays, you need to transfer the contents of array namely arr into these two arrays :
//Populate numbers array with the first SIZE1 integers from the array arr
for(int idx=0;idx<SIZE1 && idx < size;idx++) {
numbers[idx] = arr[idx];
showValue(numbers[idx]);
}
//Populate numbers2 array with the next SIZE2 integers from the array arr
for(int idx=SIZE1;idx<size && idx-SIZE1 < SIZE2;idx++) {
numbers2[idx-SIZE1] = arr[idx];
showValue(numbers2[idx-SIZE1]);
}