Please support our C++ advertiser: Programming Forums
Views: 250 | Replies: 7
![]() |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hey again!
This time I have this program which is supposed to read some stream file called "numbers.dat" which has the numbers 1 up to 10.
I get the reading, but the thing is in the for loop I want to add the numbers in circles.
For example:
1 2 3 4 5 6 7 8 9 10
I want the number 1 and number 10 to be added, then number 2 and number 9 to be added.
This addition I want to write it in the stream file called "numbers2.dat"
The program does add the numbers, but in parallel: 1+2 = 3, then 3+4=7
Here is the code.
So, could you guys give me a hint or is there some way to make the addition in circles? I think I have it already but there's some kind of mistake somewhere. Thanks!
This time I have this program which is supposed to read some stream file called "numbers.dat" which has the numbers 1 up to 10.
I get the reading, but the thing is in the for loop I want to add the numbers in circles.
For example:
1 2 3 4 5 6 7 8 9 10
I want the number 1 and number 10 to be added, then number 2 and number 9 to be added.
This addition I want to write it in the stream file called "numbers2.dat"
The program does add the numbers, but in parallel: 1+2 = 3, then 3+4=7
Here is the code.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
int size = 100;
int pos = 0;
int largo = 0;
int dato, dato2, circulo, i;
int array[size];
ifstream read("numbers.dat");
ofstream write("numbers2.dat");
read >> dato >> dato2;
while (!read.eof()) {
array[pos] = dato; // first number of the file
array[size-pos-1]= dato2; // last number of the file
pos++;
largo += 2; // how many elements are in the file
read >> dato >> dato2;
}
circulo = largo/2; // the actual number of circles
write << "LA SUMA DE TODOS LOS CIRCULOS ES: " << endl;
for (i = 0; i <= circulo; i++) {
write << "\ncirculo # " << (i+1) << ": " << dato+dato2; // write to "numbers2.dat" the addition in circles
}
return 0;
}
So, could you guys give me a hint or is there some way to make the addition in circles? I think I have it already but there's some kind of mistake somewhere. Thanks!
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,564
Reputation:
Rep Power: 40
Solved Threads: 977
1) the read loop is all screwed up
int pos = 0; while( read >> array[pos] ) pos++; // now that you have all the numbers in an array, just add them // up the way you want them
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
1) the read loop is all screwed up
int pos = 0; while( read >> array[pos] ) pos++; // now that you have all the numbers in an array, just add them // up the way you want them
You mean I need to replace this with the whole read loop? And what about the dato and dato2
int variables? I want them to read them (the numbers in the file) and save them into these variables.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,564
Reputation:
Rep Power: 40
Solved Threads: 977
you don't need dato and dato2 because the code you posted does nothing with them. You could modify the code I posted like this:
int pos = 0;
while( read >> array[pos] >> array[size-pos-1])
{
pos++;
largo += 2; // how many elements are in the file
}
// now that you have all the numbers in an array, just add them
// up the way you want them
Last edited by Ancient Dragon : Sep 28th, 2008 at 9:58 pm.
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Ok, I replaced the code with the one you gave me. I have this now:
This is what I get in the output of the file:
LA SUMA DE TODOS LOS CIRCULOS ES:
circulo # 1: 6822532
circulo # 2: 6822532
circulo # 3: 6822532
circulo # 4: 6822532
circulo # 5: 6822532
circulo # 6: 6822532
What am I doing wrong?
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
int i, circulo;
const int size = 100;
int pos = 0;
int largo = 0;
int array[size];
ifstream read("numbers.dat");
ofstream write("numbers2.dat");
while( read >> array[pos] >> array[size-pos-1])
{
pos++;
largo += 2;
}
circulo = largo/2;
write << "LA SUMA DE TODOS LOS CIRCULOS ES: " << endl;
for (i = 0; i <= circulo; i++) {
write << "\ncirculo # " << (i+1) << ": " << array[pos]+array[size-pos-1];
}
return 0;
}This is what I get in the output of the file:
LA SUMA DE TODOS LOS CIRCULOS ES:
circulo # 1: 6822532
circulo # 2: 6822532
circulo # 3: 6822532
circulo # 4: 6822532
circulo # 5: 6822532
circulo # 6: 6822532
What am I doing wrong?
Last edited by lmastex : Sep 29th, 2008 at 12:31 am.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,564
Reputation:
Rep Power: 40
Solved Threads: 977
>>write << "\ncirculo # " << (i+1) << ": " << array[pos]+array[size-pos-1];
Variable pos is never changed. I think you should use the loop counter instead of pos.
Variable pos is never changed. I think you should use the loop counter instead of pos.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode