2017-03-21 12 views
0

次のコードでは、y [i]の値をx [i]、y [0]と同じ列にx [10]の下から追加します。 x [i]の値の最初の列の次の2列目にy [i]の値を追加したいと思います。これを行う方法を教えてください。データを列に追加するにはどうすればよいですか?

#include <fstream> 
using namespace std; 
int main() { 
    ofstream outfile; 
    double x[10], y[10]; 
    for(int i=0; i<10; i++){ 
    x[i] = i+10; 
    } 
    for(int i=0; i<10; i++){ 
    y[i] = i-10; 
    } 
    outfile.open("result.txt", ios_base::app); 
    outfile << "loop: " << 1 << endl; 
    for(int i=0; i<10; i++){ 
    outfile << x[i] << "\n"; 
    } 
    outfile << "loop: " << 2 << endl; 
    for(int i=0; i<10; i++){ 
    outfile << y[i] << "\n"; 
    } 
    outfile.close(); 
return 0; 
} 
+0

の必要はありませんあなたは[ 'X [I + 1] = xのような何かをしようとしていますi + 1] + y [i];? –

答えて

0

変更

outfile << "loop: " << 1 << endl; 
for(int i=0; i<10; i++){ 
    outfile << x[i] << "\n"; 
} 

ツー

outfile << "loop: 1 \t loop: 2" << endl; 
for(int i=0; i<10; i++){ 
outfile << x[i] << " \t "<<y[i]<<"\n"; 
} 

outfile << "loop: " << 2 << endl; 
    for(int i=0; i<10; i++){ 
    outfile << y[i] << "\n";} 
0

しようとしていますか?

#include <fstream> 
using namespace std; 
int main() { 
    ofstream outfile; 
    double x[10], y[10]; 
    for(int i=0; i<10; i++){ 
    x[i] = i+10; 
    y[i] = i-10; 
    } 

    outfile.open("result.txt", ios_base::app); 
    outfile << "loop: " << 1 << endl; 
    for(int i=0; i<10; i++){ 
     outfile << x[i] << ' ' << y[i] << "\n"; 
    } 
    outfile.close(); 
    return 0; 
} 
+0

最初の2つのforループは、1つのforループに減らすことができます。 – Shiping

+0

これも完璧に動作します。私はあなたの答えをアップアップすることができませんでした。なぜなら、私はこのフォーラムの新しいメンバーであり、十分な評判がないからです。手伝ってくれてどうもありがとう。 – user3328741

+0

@ user3328741あなたの答えが得られれば大丈夫です。 – Shiping

関連する問題