Moving pieces in an array? C++ - c++

How could you move array characters?????????

Here is some basic code with high-level comments. It is not exactly as you desire. But since you have provided some code it is nearly there.
After reading the comments and understanding what is happening, it should be relatively straightforward to modify the below code to your requirements:
#include <iostream>
void printArray(int gameboard[5][5]){
std::cout << "This is what the gameboard looks like now:" << std::endl;
for ( int i = 0; i < 5; i++ ) {
for ( int j = 0; j < 5; j++ ) {
std::cout << gameboard[i][j] << ' ';
}
std::cout << std::endl;
}
}
int main() {
// Declare array and print what it looks like
int gameboard[5][5] = { {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5}};
printArray(gameboard);
// Get input for which coordinates the user wants to swap
int row1, column1, row2, column2;
std::cout << "Please enter the coordinates of the first piece:" << std::endl;
std::cout << "Row:";
std::cin >> row1;
std::cout << "Column:";
std::cin >> column1;
std::cout << "Please enter the coordinates of the second piece:" << std::endl;
std::cout << "Row:";
std::cin >> row2;
std::cout << "Column:";
std::cin >> column2;
// Swap values at provided coordinates by using a temp variable
int temp = gameboard[row1][column1];
gameboard[row1][column1] = gameboard[row2][column2];
gameboard[row2][column2] = temp;
printArray(gameboard);
return 0;
}
Example Usage:
This is what the gameboard looks like now:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Please enter the coordinates of the first piece:
Row: 0
Column: 0
Please enter the coordinates of the second piece:
Row: 4
Column: 4
This is what the gameboard looks like now:
5 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 1
Tasks TO-DO for you:
Change printArray to allow arrays of varying sizes not just 5 x 5.
Ensure user input for row, column and value are numbers.
Ensure user input row, and column values are within the bounds of the array.

Related

I wanted to reverse my array. Why this code gives garbage value?

#include <iostream>
using namespace std;
int main(){
int n;
int a[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
}
Input:- 4
1 2 3 4
Output 4199008 4 3 2 1
For starters the program has undefined behavior because the variable n is not initialized
int n;
So this declaration
int a[n];
is invalid. Moreover variable length arrays is not a standard C++ feature. Instead use the standard class template std::vector.
Also within this loop
for(int i=n;i>=0;i--) {
cout<<a[i]<<" ";
}
you are trying to access of non-existent element with the index n.
Also you are not reversing an array. You are trying to output an array in the reverse order.
Pay attention to that there are standard algorithms std::reverse and std::reverse_copy declared in the header <algorithm>.
Here is an example how your program with using your approach could look
#include <iostream>
#include <vector>
int main()
{
size_t n = 0;
std::cout << "Enter the size of an array ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
for ( auto &item : v ) std::cin >> item;
std::cout << "The array in the reverse order\n";
for ( size_t i = v.size(); i != 0; )
{
std::cout << v[--i] << ' ';
}
std::cout << '\n';
return 0;
}
The program output might look like
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
If to use standard algorithms then your program can look the following way
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
size_t n = 0;
std::cout << "Enter the size of an array ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
std::copy_n( std::istream_iterator<int>( std::cin ), n, std::begin( v ) );
std::cout << "The array in the reverse order\n";
std::reverse_copy( std::begin( v ), std::end( v ),
std::ostream_iterator<int>( std::cout, " ") );
std::cout << '\n';
return 0;
}
The program output might look the same way as shown above
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
a[n] will return the element after the last one. When you iterate in reverse order, start with i=n-1.
At the beginig of your program there is a mistake:
int n; // You declare n with no value
int a[n]; // You use is
cin>>n; // After you used it you get your value-
Now i can suppose that that was just an error while copying it because you give inputs and outputs
Input:- 4 1 2 3 4 Output 4199008 4 3 2 1
So forgeting about that, you declare an array of size n. Remember that the elemnts of the array will go from 0 to n-1. Now look at your second for loop
// the first element you acces is n and you stop at 1
// but the array goes from n-1 to 0
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
So you still get n values as an output but the first element that you access is outside of the array. Thats why you get a garbage value, that is a value that was left there.
A solution will be to change the for loop
for(int i=n-1;i>=-1;i--){
cout<<a[i]<<" ";
}
While reversing the array start the loop from n-1 that is i=n-1 (n is the no of elements in array). And run the loop till i>=0. If you will start loop from n it will read illegal index which is out of range and will give you garbage value.
for(int i=n-1; i>=0; i++){
cout<<arr[i]<<" ";}

Reading each line from a text file, word by word and converting to int (Infinite loop or crashing?)

I'm trying to read in this text file:
8 4 4 6 1
8 4 4 6 2
8 4 4 6 3
8 4 4 6 4
8 4 4 6 5
8 4 4 6 6
8 4 4 6 7
8 4 4 6 8
11 4 4 6 3
15 11 13
7 2 1 4 4
9 4 3 9 9
8 2 1 5 4
10 1 2 3 4 6 1
6 1 1 2 5 3 2
13 1 1 2 10 3 8
11 2 11 10 7
And printing it exactly as shown to the console (to make sure I got every input).
However, for some reason my code crashes after reading in the first line. I can't even terminate the debugger.
Here's my code:
while(getline(inFile, buffer)){
buffer2 = strdup(buffer.c_str());
line = strtok(buffer2, " ");
size = atoi(line);
cout << size << " ";
while(line!=NULL){
line = strtok(NULL, " ");
cout << line << " ";
}
cout << "~~~~~~~~~" << endl;
}
If you are going to use C++ you should take advantage of that, use string streams:
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std; //for sample purposes, should not be used
int main() {
int temp, count = 0, sum = 0, total = 0;
string buffer;
ifstream myFile("in.txt");
if (!myFile.is_open())
cout << "No file" << endl;
else{
while(getline(myFile, buffer)){
sum = 0;
stringstream ss(buffer);
while(ss >> temp){
count++; //number count
sum += temp; //line sum
cout << temp << " ";
}
total += sum; //total sum
cout << endl << "count: " << count << endl
<< "sum: " << sum << endl << "total: " << total << endl << endl;
}
myFile.close();
}
cout << "~~~~~~~~~" << endl;
}
You are leaking the memory allocated by strdup(). You need to call free() when you are done using buffer2.
But more importantly, strtok() returns NULL when there are no more tokens to return. But it is undefined behavior to pass a NULL char* pointer to operator<<. Your while loop is doing exactly that when it reaches the end of each line, so anything could happen, including crashing.
Try this instead:
while (getline(inFile, buffer)) {
buffer2 = strdup(buffer.c_str());
if (buffer2 != NULL) {
line = strtok(buffer2, " ");
while (line != NULL) {
size = atoi(line);
cout << size << " ";
line = strtok(NULL, " ");
}
free(buffer2);
}
cout << "~~~~~~~~~" << endl;
}
That being said, why are you using strdup(), strtok(), and atoi() at all? You are writing C++ code, you should C++ semantics instead of C semantics. For example, you can use std::istringstream instead, eg:
while (getline(inFile, buffer)) {
istringstream iss(buffer);
while (iss >> size) {
cout << size << " ";
}
cout << "~~~~~~~~~" << endl;
}
As always, there are many possible solutions. I would like to show an additional one. This is using more modern C++ elements, mainly from the algorithm and iterator library.
So, what will we do?
First we read each line as a std::string in a simple for loop with std::getline. Then we will put the line again in a std::istringstream so that we can take advantage of C++ iterator: std::istream_iterator.
This iterator will iterate over the elements in the string and extract all integers. It is like calling the extractor operator ( >> ) for all elements in the line string.
We use the iterator in the so called range constructor of os a std::vector. This inplace created vector, will be added to the destiantion data. So, as a result, we will get vector of vector of int: A 2-dimensional vector.
For debug purposes, we copy each row of intes to std::cout.
Please note that we do really need only very few and very simple statements to fulfill the task.
Please check.
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
#include <iterator>
std::istringstream sourceFile{R"(8 4 4 6 1
8 4 4 6 2
8 4 4 6 3
8 4 4 6 4
8 4 4 6 5
8 4 4 6 6
8 4 4 6 7
8 4 4 6 8
11 4 4 6 3
15 11 13
7 2 1 4 4
9 4 3 9 9
8 2 1 5 4
10 1 2 3 4 6 1
6 1 1 2 5 3 2
13 1 1 2 10 3 8
11 2 11 10 7)"};
int main()
{
// Here we will store the resulting int values
std::vector<std::vector<int>> data{};
for (std::string line{}; std::getline(sourceFile, line); ) {
// Split the line into integers and add to target array
std::istringstream iss(line);
data.emplace_back(std::vector<int>(std::istream_iterator<int>(iss), {}));
}
// Now all data is in our vector of vector of int
// Show read data on screen
std::for_each(data.begin(), data.end(), [](const std::vector<int>& v){
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n";});
return 0;
}
Please note. I do not have files on SO. So I used a std::istringstream as input stream. You may of course exchange it with any other std::ftream

Read from text file into 2D array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've got a text file which contains several lines of integers, each integer is separated by a space, I want to read these integers into an array, where each new line is the first dimension of the array, and every integer on that line is saved into the second dimension.
My text file looks something like this:
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
8 9 0 1 2 3 4 5 6 7
7 8 9 0 1 2 3 4 5 6
6 7 8 9 0 1 2 3 4 5
5 6 7 8 9 0 1 2 3 4
4 5 6 7 8 9 0 1 2 3
3 4 5 6 7 8 9 0 1 2
2 3 4 5 6 7 8 9 0 1
So here's what i tried so far, but it looks like a mess
string array[30][30]; //row, column
ifstream myfile("numbers.txt");
int row = 0;
int col = 0;
while(!myfile.eof())
{
//Extract columns
while(getline(myfile, array[row][col]),!'\n')
{
getline(myfile,array[row][col],' ');
col++;
}
//Extract rows
// getline(myfile,array[row][col],'\n');
// row++;
cout<< row << '\t' << array[row][col] << "\n";
}
while(!myfile.eof()) is rarely a good idea. When you've read your last line, that condition will still evaluate to true. eof() will only be set once you've tried to read beyond the last character in the file. Also, string array[30][30] is a hardcoded 30x30 C style array that doesn't fit your data. Instead, use the C++ container std::vector (that can be nested in as many dimensions as you'd like) to dynamically add numbers.
Assuming that you don't have blank lines in numbers.txt you could do like this:
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdexcept>
std::vector<std::vector<int>> get_2d_array_of_ints_from_stream(std::istream& is) {
std::vector<std::vector<int>> return_value;
std::string line;
while(std::getline(is, line)) { // if this fails, EOF was found or there was an error
std::istringstream iss(line); // put the line in a stringstream to extract numbers
int value; // temporary used for extraction
std::vector<int> line_values; // all values on this line
while(iss >> value) // extract like when reading an int from std::cin
line_values.push_back(value); // put the value in the 1D (line) vector
// check that all lines have the same amount of numbers
if(return_value.size() && return_value[0].size()!=line_values.size())
throw std::runtime_error("file format error");
return_value.emplace_back(std::move(line_values)); // move this line's vector<int>
// to the result_value
}
return return_value;
}
int main() {
if(std::ifstream is{"numbers.txt"}; is) {
try {
// auto arr2d = get_2d_array_of_ints_from_stream(is);
// would be the same as:
std::vector<std::vector<int>> arr2d = get_2d_array_of_ints_from_stream(is);
std::cout << "Got a " << arr2d[0].size() << "x" << arr2d.size() << " array\n";
for(const std::vector<int>& line_values : arr2d) {
for(int value : line_values) {
std::cout << " " << value;
}
std::cout << "\n";
}
std::cout << "--\n";
// or you can use the subscript style of arrays
for(size_t y = 0; y < arr2d.size(); ++y) {
for(size_t x = 0; x < arr2d[y].size(); ++x) {
std::cout << " " << arr2d[y][x];
}
std::cout << "\n";
}
} catch(const std::exception& ex) {
std::cerr << "Exception: " << ex.what() << "\n";
}
}
}

long integer multiplication in c++

I am a beginner in C++. I am trying this long integer multiplication. I am not understanding that how the value ofsum[3][0] is changing in the subsequent loop cycle.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string num1, num2;
int i,j,l1, l2,temp,k;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
l1= num1.size();
l2= num2.size();
cout << l1 << " " << l2 << endl;
int sum[l2-1][l2]; // 5 6 7 8 ---> num1
// 1 2 3 4 ---> num2
for(i=0; i<l1; i++) // ---------
num1[i]-='0'; // 2 2 7 1 2 sum[3][4]---->sum[3][0]
// 1 7 0 3 4 i.e sum[3][0] should be 2
for(i=0; i<l2; i++) // 1 1 3 5 6
num2[i]-='0'; // 5 6 7 8
// -------------
for(i=l2-1; i>=0; i--) // 7 0 0 6 6 5 2
{
k=0;
temp=0;
for(j=l1-1; j>=0; j--)
{
temp+=(num2[i]*num1[j]);
sum[i][k]= temp%10;
temp=temp/10;
k++;
}
sum[i][k]=temp;
cout << sum[3][0] << endl;
}
for(i=l2-1; i>=0; i--) // output is 2 2 7 1 1 Here value of sum[3][0] is 1 but the desired output is 2.
{ // 1 7 0 3 1
for(k=l2; k>=0; k--) // 1 1 3 5 0
cout << sum[i][k]; // 0 5 6 7 8
cout << endl;
}
return 0;
}
I tried this code for the case num1=5678 and num2=1234. So sum[3][0] should be 2 in that case.
You did not make sum large enough. You use sum[0..l2-1][0..l1] so the size would need to be sum[l2][l1+1].
When you exceed the second dim of sum that typically makes part of one row of sum share storage with part of another, so the place where you stored sum[2][0] is the same place you later stored sum[1][4]
When you exceed the first dim of sum (or the second dim in the last row) that makes sum share storage with other things, such as(but not necessarily) the other variables local to that function.
Also, your loop for displaying sum is incorrect. You use rows of sum from l2-1 down to 0 and columns from 0 up to L1. But you display columns l2 down to 0. The columns are computed based on l1, so should be displayed based on l1. That error would have symptoms if you tried an example with l1 not equal l2.
The sum array should be create like this:
int sum[l2][max(l1,l2)+1];
It is too small to store results of you calculations now.
Why program doesn't crash while you write out of bounds of the array? Because C++ has not any array bounds checking.
To be honest, you should declare the array by new and remove it by delete when it is not needed anymore. C++ standard doesn't provide to create non-constant size array without new. Only some compilers supports it as an extension and many of them prints warnings when you do that. More informations here: How do I declare a 2d array in C++ using new?

Bringing in data of two separate values from txt file into array

So I am reading in data from a text file and putting it into an array. My code works fine and outputs the data except that instead of outputting data like
Q 5
it outputs it like this
Q
5
This is part of a greater assigment where I put all the values into a queue and sort them based on the numerical value which in the example above would be 5. But I just want help trying to get the data to come out like
Q 5
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int alphabet = 52;
char letter[alphabet];
int count = 0;
ifstream dataIn;
dataIn.open("CharInput.txt");
if (!dataIn)
{
cout << "Error opening data file\n";
}
else
{
while (count < alphabet && dataIn >> letter[count])
count++;
dataIn.close();
cout << "The letters and their position are: " << endl;
for (int stuff = 0; stuff < count; stuff++)
{
cout << letter[stuff] << endl;
}
}
system("PAUSE");
return 0;
}
The data file is names CharInput.txt and has:
26
Q 5
W 3
E 8
R 7
T 2
Y 9
U 0
I 9
O 6
P 1
A 2
S 2
D 4
F 3
G 6
H 9
J 8
K 0
L 3
Z 1
X 5
C 7
V 4
B 7
N 2
M 8
The data are output in lines because you are printing them ending with std::endl, which by its name, ending current line.
Instead, use a white space between each element and its count:
for (int stuff = 0; stuff < count; stuff += 2)
{
cout << letter[stuff] << " " << letter[stuff + 1] << std::endl;
}