2017-03-08 4 views
0

この割り当てでは、関数テンプレートを使用してキーボードから配列データを読み込み、各配列に2つの最小値を出力し、配列を降順でソートし、データをテキストファイルに変換し、そのテキストファイルを取得します。 私の主な問題は、私のsmallPrint関数テンプレートの周りにエラーが発生し続けることです。 (float [7]、int、int、const int) プログラムを実行しようとするとポップするもう1つのことは、問題は、私のプリントアレイ関数テンプレート内にある私が間違って何も表示されませんC++:関数テンプレートを使用して配列を編集する問題

私は本当にまったく助けに感謝

これは代入です。。。

「除去することにより、arrtemp.cppを変更します配列ごとにデータを再割り当てし、5つの新しい関数テンプレートを追加します。 1.)最初の新しい関数テンプレートは、ユーザーがキーボードから配列データを入力できるようにする必要があります。 2.)2番目の新しい関数テンプレートは、並べ替えを行わずに最小値と最小値の2番目の値を出力します。各配列は個別のデータで構成されていると仮定できます。 3.)3番目の新しい関数テンプレートは、降順で配列をソートする必要があります。 4.)4番目の新しい関数テンプレートは、保存データをテキストファイルに保存する必要があります。 5.)5番目の新しい関数テンプレートは、テキストファイルから配列データを取得する必要があります。出力には、各アレイの最小値と最小値の2つが含まれていなければならず、3つのアレイは、テキストファイルが保存される前に1回、取り出された後に1回、降順で2回印刷されます。各関数テンプレートの前に[template]を含めてください。各配列は別のテキストファイルに保存する必要があります。また、すべての3つのテキストファイルを表示する必要があります「の下

がarrtemp.cppと私は何を書かれています:。

#include<iostream> 
#include<fstream> 
using namespace std; 

template <class T> 
T loadArray(T *a, const int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     cin >> a[i]; 
    } 
} 

template <class T> 
T smallPrint(T *a, T *small, T *small2, const int n) 
{ 
    small = a[0]; 
    small2 = a[0]; 
    for (int i = 1; i < n; i++) 
    { 
     if (a[i] < small) 
      small = a[i];  
    } 
    for (int i = 1; i < n; i++) 
    { 
     if (a[i] > small && a[i] < small2) 
      small2 = a[i]; 
    } 
    cout << small << ' ' << small2; 
    cout << endl << endl; 
} 

template <class T> 
T sort(T *a, const int n) 
{ 
    for (int i = 0; i < n; i++) 
     for (int j = 0; j < n; j++) 
    { 
     if (a[j] < a[j + 1]) 
     { 
      T t = a[j]; 
      a[j] = a[j + 1]; 
      a[j + 1] = t; 
     } 
    } 
} 

template <class T> 
T saveFile(T *a, T small, T small2, const int n, string filename) 
{ 
    ofstream outfile(filename, ios::out); 
    for (int i = 0; i < n; i++) 
    { 
     outfile << a[i] << ' '; 
    } 
    outfile << small << small2; 
    outfile.close(); 
} 

template <class T> 
T retrieveFile(T *a, T small, T small2, const int n, string filename) 
{ 
    ifstream infile(filename, ios::in); 
    for (int i = 0; i < n; i++) 
    { 
     infile >> a[i]; 
    } 
    infile >> small >> small2; 
    infile.close(); 
} 

template <class T> 
T printArray(T*a, const int n) 
{ 
    for (int i = 0; int < n; i++) 
     cout << a[i] << ' '; 
    cout << endl; 
} 

int main() 
{ 
    const int n1 = 5, n2 = 7, n3 = 6; 
    int a[n1]; 
    float b[n2]; 
    char c[n3]; 
    int smalli, smalli2; 
    float smallf, smallf2; 
    char smallc, smallc2; 

    loadArray(a, n1); 
    loadArray(b, n2); 
    loadArray(c, n3); 

    smallPrint(a, smalli, smalli2, n1); 
    smallPrint(b, smallf, smallf2, n2); 
    smallPrint(c, smallc, smallc2, n3); 


    sort(a, n1); 
    sort(b, n2); 
    sort(c, n3); 

    printArray(a, n1); 
    printArray(b, n2); 
    printArray(c, n3); 

    saveFile(a, smalli, smalli2, n1, "i:\\ints.txt"); 
    saveFile(b, smallf, smallf2, n2, "i:\\floats.txt"); 
    saveFile(c, smallc, smallc2, n3, "i:\\chars.txt"); 

    retrieveFile(a, smalli, smalli2, n1, "i:\\ints.txt"); 
    retrieveFile(b, smallf, smallf2, n2, "i:\\floats.txt"); 
    retrieveFile(c, smallc, smallc2, n3, "i:\\chars.txt"); 

    printArray(a, n1); 
    printArray(b, n2); 
    printArray(c, n3); 

    system("PAUSE"); 
    return 0; 
} 
を:私が書いた何

arrtemp.cpp

#include <iostream> 
using namespace std; 

//function template to print an array 
//works for multiple data types 

template <class T> 
void printarray (T *a, const int n) 
{ 
    for (int i = 0; i < n; i++) 
     cout << a[i] << " "; 
    cout << endl; 
} 

int main() 
{ 
    const int n1 = 5, n2 = 7, n3 = 6; 
    int a[n1] = {2, 4, 6, 8, 10}; 
    float b[n2] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; 
    char c[n3] = "HELLO"; 

    cout <<"the integer array" << endl; 
    printarray(a, n1); 

    cout <<"the float array" << endl; 
    printarray(b,n2); 

    cout <<"the string is" << endl; 
    printarray(c,n3); 
    return 0; 
} 


これは動作する修正版です:

#include<iostream> 
#include<fstream> 
using namespace std; 

template <class T> 
void loadArray(T *a, const int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     cout << "Enter a number (" << n-i << " left): "; 
     cin >> a[i]; 
    } 
    cout << endl; 
} 

template <class T> 
void smallPrint(T *a, T &small, T &small2, const int n) 
{ 
    small = a[0]; 
    small2 = a[0]; 
    for (int i = 1; i < n; i++) 
    { 
     if (a[i] < small) 
      small = a[i]; 
    } 
    for (int i = 1; i < n; i++) 
    { 
     if (a[i] > small && a[i] < small2) 
      small2 = a[i]; 
    } 
    cout << small << ' ' << small2; 
    cout << endl << endl; 
} 

template <class T> 
void sort(T *a, const int n) 
{ 
    for (int i = 0; i < n; i++) 
     for (int j = 0; j < n; j++) 
     { 
      if (a[j] < a[j + 1]) 
      { 
       T t = a[j]; 
       a[j] = a[j + 1]; 
       a[j + 1] = t; 
      } 
     } 
} 

template <class T> 
void saveFile(T *a, T small, T small2, const int n, string filename) 
{ 
    ofstream outfile(filename, ios::out); 
    for (int i = 0; i < n; i++) 
    { 
     outfile << a[i] << ' '; 
    } 
    outfile << small << ' ' << small2; 
    outfile.close(); 
} 

template <class T> 
void retrieveFile(T *a, T small, T small2, const int n, string filename) 
{ 
    ifstream infile(filename, ios::in); 
    for (int i = 0; i < n; i++) 
    { 
     infile >> a[i]; 
    } 
    infile >> small >> small2; 
    infile.close(); 
} 

template <class T> 
void printArray(T*a, const int n) 
{ 
    for (int i = 0; i < n; i++) 
     cout << a[i] << ' '; 
    cout << endl; 
} 

int main() 
{ 
    const int n1 = 5, n2 = 7, n3 = 6; 
    int a[n1]; 
    float b[n2]; 
    char c[n3]; 
    int smalli, smalli2; 
    float smallf, smallf2; 
    char smallc, smallc2; 

    loadArray(a, n1); 
    loadArray(b, n2); 
    loadArray(c, n3); 

    smallPrint(a, smalli, smalli2, n1); 
    smallPrint(b, smallf, smallf2, n2); 
    smallPrint(c, smallc, smallc2, n3); 


    sort(a, n1); 
    sort(b, n2); 
    sort(c, n3); 

    printArray(a, n1); 
    printArray(b, n2); 
    printArray(c, n3); 

    saveFile(a, smalli, smalli2, n1, "i:\\ints.txt"); 
    saveFile(b, smallf, smallf2, n2, "i:\\floats.txt"); 
    saveFile(c, smallc, smallc2, n3, "i:\\chars.txt"); 

    retrieveFile(a, smalli, smalli2, n1, "i:\\ints.txt"); 
    retrieveFile(b, smallf, smallf2, n2, "i:\\floats.txt"); 
    retrieveFile(c, smallc, smallc2, n3, "i:\\chars.txt"); 

    printArray(a, n1); 
    printArray(b, n2); 
    printArray(c, n3); 

    system("PAUSE"); 
    return 0; 
}` 

答えて

0

T smallPrint(T *a, T *small, T *small2, const int n)T smallPrint(T *a, T small, T small2, const int n)にする必要があります。 printArrayfor (int i = 0; i < n; i++)の代わりにfor (int i = 0; int < n; i++)があります。

+0

ありがとうございます!そのような愚かな間違いは、本当にプログラムを台無しにする可能性があります。私は 'void sort(T * a、const int n)'に設定する必要があるときに、すべての関数で 'T sort(T * a、const int n)'を持っていたことに気付きました。 –

+0

@PaulIあなたは私の答えを受け入れるか、あなたの質問を解決したものとしてマークすることができます。 –

関連する問題