How to declare the array and read the array? Must I use data structure?
Two array
1 30
2 20
3 10
4 50
5 15
How to declare the array and read the array? Must I use data structure?
Two array
1 30
2 20
3 10
4 50
5 15
The file you showed is just two columns of integers (id value). The main problems in the posted code are simple and common: the read-index i is never initialized, the same name myfile is reused for an output stream (shadowing the input stream), loops use array contents as loop bounds instead of the actual count read, and comparisons like exams[i+1] or …[j+1] will go out of bounds on the last element. pointed to the right idea (store rows, then iterate by the number actually read); follow that flow and the program will behave predictably.
Recommended, safe workflow (step by step)
Example (concise, safer approach)
std::vector<std::pair<int,int>> rows;
int a,b;
std::ifstream in("STA83SOLUTION.txt");
if (!in) /* handle error */;
while (in >> a >> b) rows.emplace_back(a,b);
std::sort(rows.begin(), rows.end(), [](auto const& x, auto const& y){ return x.second > y.second; });
for (auto const &p : rows) std::cout << p.first << " " << p.second << '\n'; Quick debugging checklist
int i = 0;) before use.count and use it for all loops.Jump to Post— Ancient Dragon 5,243Are you STILL on that same program ? Is that the same problem you posted 185 posts ago ? Haven't you learned anything yet ?
Jump to Post— Ancient Dragon 5,243you should know by now how to open and read the file. All you have to do is make a 2d array of integers
int array[255][2] = {0};That will hold up to 255 rows of data
Jump to Post— hacker9801 49.......it's not hard at all.
(edit: lol a little late, sorry I was reading the other thread)
Jump to Post— Ancient Dragon 5,243>>Just change it like this to make 2d array?
NO NO.int main() { int array[140][2] = {0}; ifstream myfile("filename.txt"); int i = 0; while( myfile >> array[i][0] >> array[i][1] ) i++; myfile.close(); }
Are you STILL on that same program ? Is that the same problem you posted 185 posts ago ? Haven't you learned anything yet ?
yes...but it was so complicated..i want to read as simple as possible.
I'm having problem when i want to cout the data that i sort.
int temp,bubsort,arr,display;
if (myfile.is_open())
{
for (int i = 0; i < exams.size(); i++)
{
for (int j = 0; j<exams.at(i).total.size(); j++)
{
cout<<"\n"<<i+1<<":"<<" "<< exams[i].total[j]<<"\t"; // output list of exam codes for this student
if (exams[i+1].total[j+1]>exams[i].total[j])
{
temp=exams[i+1].total[j+1];
exams[i+1].total[j+1]=exams[i].total[j];
exams[i].total[j]=temp;
}
bubsort(arr,j);
display(arr,j);
cout<<endl<<endl;
}
}
}
cin.get();
return 0;
}
} you should know by now how to open and read the file. All you have to do is make a 2d array of integers int array[255][2] = {0}; That will hold up to 255 rows of data
.......it's not hard at all.
(edit: lol a little late, sorry I was reading the other thread)
Just change it like this to make 2d array?
int array[140][2]={0};
int temp;
if (myfile.is_open())
{
for (int i = 0; i < exams.size(); i++)
{
for (int j = 0; j<exams.at(i).total.size(); j++)
{
cout<<"\n"<<i+1<<":"<<" "<< exams[i].total[j]<<"\t"; // output list of exam codes for this student
if (exams[i+1].total[j+1]>exams[i].total[j])
{
temp=exams[i+1].total[j+1];
exams[i+1].total[j+1]=exams[i].total[j];
exams[i].total[j]=temp;
} >>Just change it like this to make 2d array?
NO NO.
int main()
{
int array[140][2] = {0};
ifstream myfile("filename.txt");
int i = 0;
while( myfile >> array[i][0] >> array[i][1] )
i++;
myfile.close();
} Why no output is appear?
#include <iostream> // std::cout
#include <fstream>
#include <iomanip>
#include <string> // std::string
#include <vector> // std::vector<>
#include <algorithm> //std::for each()
#include <iostream> // std::cout
#include <fstream>
#include <iomanip>
#include <string> // std::string
#include <vector> // std::vector<>
#include <algorithm> //std::for ea
using namespace std; // import "std" namespace into global namespace
int main()
{
int array[140][2] = {0};
ifstream myfile("STA83SOLUTION.txt");
int i;
while( myfile >> array[i][0] >> array[i][1] )
i++;
{
ofstream myfile;
myfile.open("411.txt");
myfile.close();
}
if (myfile.is_open())
{
for (int i = 0; i < array[i][0]; i++)
{
for (int j = 0; j<array[i][0]; j++)
{
cout<<"\n"<<i+1<<":"<<" "<< array[i][0]<<"\t"; // output list of exam codes for this student
}
}
}
} F:\508.cpp(35) : warning C4508: 'main' : function should return a value; 'void' return type assumed
>>Why no output is appear
Think about what you are doing. Look at lines 30 and 35. Can you guess the value of array[0] on the first iteration of that loop ? Hint: see line 19.
F:\508.cpp(35) : warning C4508: 'main' : function should return a value; 'void' return type assumed
add return 0; at the end of main()
So I must chane [0] to [140][2]?
int array[140][2] = {0};
ifstream myfile("STA83SOLUTION.txt");
int i;
while( myfile >> array[i][0] >> array[i][1] )
i++;
{
ofstream myfile;
myfile.open("508.txt");
myfile.close();
}
if (myfile.is_open())
{
for (int i = 0; i < array[140][2]; i++)
{
for (int j = 0; j<array[140][2]; j++)
{
cout<<"\n"<<i+1<<":"<<" "<< array[i][2]<<"\t"; // output list of exam codes for this student
}
}
}return 0; >>So I must chane [0] to [140][2]?
No. Just forget it. You have no clue what you are doing.
so..I must continue this programming or continue the old one?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.