Just a heads up: My c++ programming skills and terminology is intermediate at best. So please be gentle ;).
I am working on a multi-sort algorithm for a college class. Originally, I built the program to take in an array of 20 integers, since that was as big as the .txt files were. The final lab is now asking to take in files that have 10, 100, 1000, 10000, 100000 and 1000000 different numbers. I originally used an ifstream inside a for loop to read in the ints. Now that I need to read a variable amount of ints from a file, I have run into issues with this code. I have extensively searched this site and Google to find an answer to this problem. I have tried dozens of different code snippets, to no avail. Here is the code I am currently running that works for 20 ints.
int i;
int A[20];
int length;
char unsortedFilename[200];
ifstream unsorted;
cout << "Please type the full name of the file you would like sorted.\n* ";
cin >> unsortedFilename;
unsorted.open(unsortedFilename);
length = (sizeof(A) / sizeof(*A));
for( i = 0; i < length; i++ )
{
unsorted >> A[i];
cout << A[i] << "\n";
}
insertionSort();
I do have other code mixed in there, but it's error checking, selection of duplicate number removal, etc. I would like it so that code like this would run "i" number of times, where "i" is actually the number of ints in the file. Also, as I mentioned earlier, I will need to input a file that has 1,000,000 numbers in it. I don't believe that an int array will be able to hold that many numbers. Is it going to be as easy as swapping all my ints over to longs?
Thanks for any help you could provide.
As suggested in the comments, use std::vector<int> instead of an array.
Instead of a for loop, use a while loop. Break out of the while loop when there are no numbers to read.
The while loop:
std::vector<int> A;
int item;
while ( unsorted >> item )
{
A.push_back(item);
}
You can sort the std::vector by using std::vector::iterator or simply access the data through the int* returned by A.data().
You can simply read all the numbers into a vector. Then use the vector as you would have used the array.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::string unsortedFilename;
std::cout << "Please type the full name of the file you would like sorted.\n";
std::cin >> unsortedFilename;
std::ifstream is(unsortedFilename);
std::istream_iterator<int> start(is), end;
std::vector<int> A(start, end);
std::cout << "Read " << A.size() << " numbers" << std::endl;
}
What you want is a vector.
try this,
int i;
vector<int> A;
int length;
string unsortedFilename;
ifstream unsorted;
cout << "Please type the full name of the file you would like sorted.\n* ";
cin >> unsortedFilename;
unsorted.open(unsortedFilename);
int temp;
for( i = 0; unsorted >> temp; i++ )
{
A.push_back(temp);
cout << A[i] << "\n";
}
insertionSort();
A vector is basically a dynamic array. It automatically grows as more space is needed. That way it doesn't matter if you have 10, 100, or even 100000 items, it'll automatically grow for you.
Also use a string for your file name, some file names are longer than 200 characters.
Good Luck!
will need to input a file that has 1,000,000 numbers in it. I don't believe that an int array will be able to hold that many numbers.
Sure it can. 1 Million ints is ~4Mb of memory, which is a trivial amount. You can even declare it static just as you do now int A[1000000];.
But real problem is that you're assuming a fixed length in your code, rather than determine the length from the input. I guess this is what your assignment is trying to teach you, so I won't show you the solution. But consider using ifstream::eof and make your sort accept the length as an argument...
Related
I am trying to solve the following question:
Write a program, which will get from the user
the Number of flights (N)
the destination of train (city)
the train number for certain destination (number)
the name of the wanted destination city, for which we are searching possible trains.
The program should find and display all numbers of trains, which go to wanted destination. If there is no such train, the program must display "Unreachable city!".
Now the problem is that I wrote a code which finds such train number, but not numbers of all trains, it cannot display all the train numbers going to wanted destination point.
I.e. if I input following data:
3
Chicago I-789
Chicago J-159
Chicago A-465
Chicago
It shows me only last train number A-465, whereas the right answer would be: I-789 J-159 A-465
#include <iostream>
#include <string>
using namespace std;
class MyClass {
public:
string city;
string number;
};
int main() {
int N;
cin >> N;
MyClass myObj;
for (int i=0; i<N; i++){
cin>>myObj.city;
cin>>myObj.number;
}
string destination;
cin >> destination;
if(myObj.city==destination){
cout << myObj.number;
}
else{
cout << "Unreachable city!";
}
return 0;
}
On your comment:
C++ is much harder than python.
Programming languages are just tools. You use them to solve a problem. If you don't know how to use a tool, you can't solve the problem. Your computer is a tool, if you don't know how to operate it, you can't do your homework. That doesn't mean computers are difficult to use. Similarly, C++ is a tool, if you don't know it, it doesn't mean it's difficult.
Let's get to the problem.
The problem
The program should find and display all numbers of trains, which go to wanted destination. If there is no such train, the program must display "Unreachable city!".
Let's break it down
Reading the problem carefully, we can see that there is not just "one" train but multiple 'trains'.
We have to take input from the user (wanted destination)
Then we have to find "all" the "trains" that go that city.
If no train was found, we print "Unreachable city!"
The problem with your code
The problem with your code is that there is only "one train":
MyClass myObj; //one object only
You keep overwriting it's values every time you take input from the user.
Learning the tool
So, What can you do to fix this? In programming when we want to store multiple values of the same object we usually create an array. An array is just a collection of values of one type. Example:
int myarray[5]; //can store 5 "int" values
//size is given inside the [] (square brackets)
Array indexes start from 0. We can store values in array like following:
cin >> myarray[0]; //take input from user and store it into the "first" place in our array
cin >> myarray[1]; //store in the "second" place
cin >> myarray[4]; //store in the "last" place
cin >> myarray[5]; //WRONG! Don't do this. It will result in errors and bugs!! (Undefined Behaviour)
You can also store values directly:
int myarray[5] = {1, 2, 3, 4, 5};
cout << myarray[3]; // prints "4"
That's all nice and fine however there is a small problem with arrays. We have to know the "size" of the array before we create it.
int N;
cin >> N;
int array[N]; //WRONG, even it works, this is wrong.
So, what should we do? We can't know the number of objects we want always. Worry not, because C++ provides us with a nice container: std::vector which can be used to solve this issue.
#include <vector> // you need this for vector
int N;
cin >> N;
std::vector <int> myvector(N); //vector of size N
//access the values as you would with the array
myvector[0] = 10;
myvector[5] = 9; //WRONG.
Solving your problem
Note, that I will not give you the solution directly, but I will show you the way and give you the tools. It's your problem, it's your challenge, and if you try, it's pretty easy to solve the problem.
So we learned about vectors and arrays. Next, you may be wondering how to create vector for your type. Simple:
//create a vector, with size = N
vector <MyClass> Trains (N);
//take input from user
for (int i=0; i<N; i++){
cin >> Trains[i].city;
cin >> Trains[i].number;
}
The last part, will be quite similar. You need a loop, then go over all the values in the vector to find the "destinations" you want.
Side note
You should start with naming your objects and variables in a way that it is easy and natural for you to think about your problem. For example:
class MyClass
This doesn't tell anyone, anything about your class or what you want to do with it. What could be a better name for it? Looking at the problem, I suggest the name Train:
class Train {};
The problem also tells us that each train has a "destination city" and a "train number". We can refactor our Train class to contain the following:
class Train {
public:
string destination;
string number;
};
First, myObj is not a good name, let's change it to an empty list of destinations.
#include <vector>
...
vector <MyClass> destinations;
Next, push each new value into the vector. For this it would be better to have a constructor that sets the values. Constructing a destination with no values is pointless.
MyClass(string _c, string _n) : city(_c), number(_n) {};
...
string city, number;
cin >> city;
cin >> number;
destinations.pushback(MyClass(city, number));
Now you can write your loops to go through the vector looking for the data you need.
#include <iostream>
#include <string>
using namespace std;
#include <vector> // you need this for vector
class Train {
public:
string city;
string number;
};
int main(){
int N;
cin >> N;
string dest;
vector <Train> Trains (N);
int i;
//take input from user
for (i=0; i<N; i++){
cin >> Trains[i].city;
cin >> Trains[i].number;
}
cin >> dest;
for(i=0; i<Trains.size(); i++){
if(Trains[i].city==dest){
cout << Trains[i].number <<" ";
}
else{
cout << "Unreachable city!";
}
}
}
Now it is always printing Unreachable city next to right results :(
I am new to programming and I want to get an unlimited input of numbers from user, and I need to compare each number to 4 or 7; if the number is 4 or 7, I will increase counter.
The issue is that the if condition if(arr[i]!='4' || arr[i]!='7') is executed even if the number is 4 or 7.
I tried to implement another if by comparing ASCII code >>> if(arr[i]!=0x34 || arr[i]!=0x37) but this solution also doesn't work.
Can you help me to find the issue?
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
int counter=0;
char arr[]={};
cin >> arr;
for (int i=0 ; i<strlen(arr)-1 ; i++)
{
if(arr[i]!='4' || arr[i]!='7')
{
cout << "NO" << endl;
counter=0;
break;
}else
counter++;
}
cout << counter << endl;
if(counter==4 || counter==7)
cout << "YES" << endl;
}
Your program has undefined behavior, because this
char arr[]={};
creates an array of length zero. This is not even allowed in standard C++. If your compiler didn't complain about it, then that is because it is using a non-standard language extension.
In any case, the array has length zero. You then try to write to it with
cin >> arr;
which causes the array to be accessed out-of-bounds, no matter how long the given input is. This will cause undefined behavior and you will not have any guarantee whatsoever on how the program will behave.
Never use char arrays to store strings, especially not user input. Use std::string instead:
std::string arr;
cin >> arr;
Then instead of strlen(arr) you should use arr.size().
I am pretty sure you have further logic errors in your program (see question comments). But before you fix the program logic, you should make sure that you are writing a valid program in the first place.
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
(Source: http://codeforces.com/problemset/problem/71/A)
How would you get input from the user given n? I tried using a while loop but it doesn't work:
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i;
while (i<=n) {
cin>>i ;
i++;
}
}
You probably meant to have something like:
#include <iostream>
int main() {
int n;
cin>>n;
int theInputNumbers[n];
for(int i = 0; i<n; ++i) {
cin >> theInputNumbers[i];
}
}
Your loop is really quite far off of what you need. What you wrote is extremely wrong such that I cannot provide advice other than to learn the basics of loops, variables, and input. The assistance you need is beyond the scope of a simple question/answer, you should consider buying a book and working through it cover to cover. Consider reading Programming Principles and Practice Using C++
Here is a working example of something approximating your question's requirements. I leave file input and output as an exercise up to you. I also make use of C++11's front and back std::string members. You would have to access via array index in older versions.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
int totalWords;
cin >> totalWords;
stringstream finalOutput;
for (int i = 0; i < totalWords; ++i){
string word;
cin >> word;
if (word.length() > 10){
finalOutput << word.front() << (word.length() - 2) << word.back();
}else{
finalOutput << word;
}
finalOutput << endl;
}
cout << endl << "_____________" << endl << "Output:" << endl;
cout << finalOutput.str() << endl;
}
With that said, let me give you some advice:
Name your variables meaningfully. "int i" in a for loop like I have above is a common idiom, the "i" stands for index. But typically you want to avoid using i for anything else. Instead of n, call it totalWords or something similar.
Also, ensure all variables are initialized before accessing them. When you first enter your while loop i has no defined value. This means it could contain anything, and, indeed, your program could do anything as it is undefined behavior.
And as an aside: Why are you reading into an integer i in your example? Why are you then incrementing it? What is the purpose of that? If you read in input from the user, they could type 0, then you increment by 1 setting it to 1... The next iteration maybe they'll type -1 and you'll increment it by 1 and set it to 0... Then they could type in 10001451 and you increment by 1 and set it to 10001452... Do you see the problem with the logic here?
It seems like you are trying to use i as a counter for the total number of iterations. If you are doing this, do not also read input into i from the user. That completely undermines the purpose. Use a separate variable as in my example.
This is what I have to do:
A teacher has asked all her students to line up single file according to their first name. For example, in one class Amy will be at the front of the line and Yolanda will be at the end. Write a program that prompts the user to enter the number of students in the class, then loops to read in that many names. Once all the names have been read in it reports which student wourld be at the front of the line and which one would be at the end of the line. You may assume that no two students have the same name. Input Validation: Do not accept a number less than 1 or greater than 25 for the number of students.
This is what I have so far:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int StudentNum;
cout << "How many student are in the class?\n";
cin >> StudentNum;
char sname[StudentNum + 1][25];
if (StudentNum < 1 || StudentNum > 25)
{
cout << "Please enter a number between 1-25 and try again\n";
return 0;
}
for (int i = 1; i <= StudentNum; i++);
{
cout << "Please enter the name of student #" << i << endl;
cin >> sname[i];
}
for (int output = 0; output <=StudentNum; output++);
{
cout << endl << sname[output] << endl;
}
system ("pause");
return 0;
}
Am I missing something about arrays??
You cannot create such an array because its length has to be known at compile time (i.e., it cannot be the result of an expression such as StudentNum + 1).
You can solve this issue because by the problem definition you know an upper bound for the array size, so you can use that as a compile time constant.
However, this problem can be solved without using an array at all. Read the wording carefully.
Hint for the solution without arrays: Think of the array as a single piece of paper (variable) with all the names written one after another. Not using an array then means that you have to be able to solve the problem without looking at all the names at once. How would you come to the answer if I only allowed you to see the names one by one?
Another hint: The problem is still solvable if there were several trillion students in the class (with unique names no less), i.e. more than could possibly fit in the computer's memory at any one time.
C++ array dimensions must be known at compile time (ie not dependent on user-entered variables at run-time). Use strings instead:
string sname[25];
If you were using something besides char arrays, you could also use a vector.
Think about what the problem statement is actually asking for. Your program only needs to output the first and last names alphabetically. Do you actually need to store all the names to do that?
Just for fun, here's how I would do it. Don't turn this in unless are ready to explain to your teacher how it works.
struct MinMax {
std::string min;
std::string max;
MinMax& operator+(const std::string& kid) {
if( min.empty() || kid < min) min = kid;
if( max.empty() || kid > max) max = kid;
return *this;
}
};
int main() {
int nKids;
std::cout << "How many students? " << std::flush;
std::cin >> nKids;
std::cout << "Enter students' names, followed by EOF\n";
MinMax mm(std::accumulate(
std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
MinMax()));
std::cout << mm.min << ", " << mm.max << "\n";
}
#include<iostream>
using namespace std;
int main()
{
char arr[200];
while(1) {
cin >> arr;
int i = sizeof(arr);
cout << "The arr input is "<< arr
<< " and the size of the array is "<< i << endl;
}
return 0;
}
For the input of 34,
This code outputs :The arr input is 34 and the size of the array is 200
while I want it to get the size of the used space of the array . So for The last input i want it to output :The arr input is 34 and the size of the array is 2
Can someone tell me how?
Maybe you want strlen(arr) here. It must be null terminated, otherwise the cout << arr would not have worked.
You would need to #include <cstring>
There's no automatic way to do what you want in the general case - you'll need to keep track somehow, either with your own counter, or by seeding the array with an 'invalid' value (that you define) and search for to find the end of the used elements (that's what the '\0' terminator character in a C-style string is).
In the example code you posted, the array should receive a null terminated C-style string, you can use that knowledge to count the number of valid elements.
If you're using C++ or some other library that has some more advanced data structures, you may be able to use one that keeps track of this kind of thing for you (like std::vector<>).
the size of the used space of the array
There is no such thing. If you have an array of 200 chars, then you have 200 chars. Arrays have no concept of "used" and "unused" space. It only works with C-strings because of the convention that those are terminated by a 0 character. But then again, the array itself cannot know if it is holding a C-string.
in a less involved manner, you can just count through each character till you hit a null with just a while loop. It will do the exact same thing strlen() does. Also, in practice, you should do type checking with cin, but i'll assume this was just a test.
#include <iostream>
using namespace std;
int main()
{
char arr[200];
int i;
while(1) {
cin >> arr;
i=0;
while (arr[i] != '\0' && i<sizeof(arr))
i++;
cout << "The arr input is "<< arr
<< " and the size of the array is "<< i << endl;
}
return 0;
}
Just for completeness, here is a much more C++ like solution that is using std::string instead of a raw char array.
#include <iostream>
#include <string>
int
main()
{
while (std::cin.good()) {
std::string s;
if (std::cin >> s) {
std::cout
<< "The input is " << s
<< " and the size is " << s.length()
<< std::endl;
}
}
return 0;
}
It doesn't use an array, but it is the preferable solution for this kind of problem. In general, you should try to replace raw arrays with std::string and std::vector as appropriate, raw pointers with shared_ptr (scoped_ptr, or shared_array, whatever is most appropriate), and snprintf with std::stringstream. This is the first step to simply writing better C++. You will thank yourself in the future. I wish that I had followed this advice a few years ago.
Try it
template < typename T, unsigned N >
unsigned sizeOfArray( T const (&array)[ N ] )
{
return N;
}