I am stuck on the last few problems of an array exercise. Could anyone lend a hand?
Write C++ statements that do the following:
store 5 in the first column of an array and make sure that the value in each subsequent column is twice the value in the previous column.
print the array one row per line.
print the array one column per line.
I think this will work for question #2:
for (row = 0; row < 10; row++)
{
for (col = 0; col < 20; col++)
cout << alpha[row][col] << " ";
cout << endl;
}
but question 1 and 3 have me stumped. thanks
Here's what i came up with after your tips. thanks everyone
3.
for (col = 0; col < 20; ++col)
{
for (row = 0; row < 20; ++row)
cout << alpha[row][col] << " ";
cout << endl;
}
1.
for (row = 0; row < 10; row++)
alpha[row][0] = 5;
for (col = 1; col < 20; col++)
for (row = 0; row < 10; row++)
alpha[row][col]=alpha[row][col-1]*2;
For #1, run a loop that starts at zero and goes until the number of rows. In each iteration just assign 5 to array[row][0]=5 (since coloumn 0 is the first coloumn).
Now run a loop from 1 to the number of coloumns. Inside, run another loop for each row. just assign array[row][col]=array[row][col-1]*2.
For #3, simply reverse the order of the loops. We iterate over all coloumns, and for each coloumns we have to iterate over all rows and print a newline after that.
I would post code, but it is better for you to try to understand and write the code yourself.
for each row, insert 5 into the first column (index 0), then in a loop, iterate from 1 through the number required, and the value at the current column index = 2 * value at previous column index (i.e. col - 1).
re-arrange the row, col loops.
Well for 1, just take the previous col and multiple by 5. So when your going through a loop, it'd be like col[position your at now] = col[prev pos]*2
For question #3, just reverse the order of loops, as
for (col = 0; col < 20; col++)
{
for (row = 0; row < 10; row++)
cout << alpha[row][col] << " ";
cout << endl;
}
Isn't it simple?
For question #1, just use the same reverse order of loops, and do this
int value = 5;
for (col = 0; col < 20; col++)
{
for (row = 0; row < 10; row++)
alpha[row][col] = value;
value = 2 * value;
}
Related
I have a multidimensional vector of chars, and I want to be able to delete a specific column or row on command. For example, if I had this matrix:
A B C D
L K T M
A M T N
Deleting the second column would change the matrix to
A C D
L T M
A T N
Then, deleting the third row would change the matrix to
ACD
LTM
My code that I have written for this purpose currently returns an error for both row and column deletions:
void verticalSearch(vector< vector<char> >matrix, int startIndex, int lengthWord, string word)
{
for(int k = 0; k < matrix[0].size() ; k++) // iterating for each column
{
for (int i = 0; i < matrix.size() - lengthWord + 1; i++) // for each row
{
char c;
string s = "";
for (int j = startIndex; j < startIndex + lengthWord; j++) // this startIndex is always 0
{ // but this for loop stands for iterating in a length of given word
c = matrix[i + j][k]; // adding given index of matrix to character c
s += c;
}
if (s == word) // if a specific word is founded
{
matrix[0].erase(matrix[0].begin() + k); // delete this column but not working correctly
cout << "Word is founded" << endl;
printMatrix(matrix); // And print it
}
}
}
}
Can somebody please tell me what's wrong?
You have a vector of rows, therefore to delete a column you must delete one character from each row. You can write a loop to do this. E.g.
for (int row = 0; row < matrix.size(); ++row)
matrix[row].erase(matrix[row].begin() + k);
EDIT
So your code should end up looking like this
if (s == word) // if a specific word is founded
{
// delete a column
for (int row = 0; row < matrix.size(); ++row)
matrix[row].erase(matrix[row].begin() + k);
cout << "Word is founded" << endl;
printMatrix(matrix); // And print it
}
I'm trying to take the product of all of the diagonal numbers in a 4x4 array. I know how to grab the numbers and print them, but I'm not sure how to take the product of them, how would I make it calculate the product of the 8 numbers?
#include <iostream>
using namespace std;
for (int row = 0; row < 4; row++)
{
for (int column = 0; column < 4; column++)
{
if (row==column || row == 3 - column)
{
double product = 1;
product *= arr[row][column]
cout << product << ".";
}
}
}
Note:
There are only 7 diagonal elements. You are counting the element in the centre of the matrix twice.
you don't need to iterate the whole array in order to know about the diagonals. The diagonals have a nice property of row == column as you have observed and you only need to iterate along the diagonals.
To make things clearer and easier, calculate the two diagonal products separately:
double product = 1;
for (int row = 0; row < 4; row++) {
product *= arr[row][row]
}
for (int row = 0; row < 4; row++) {
product *= arr[row][4 - (row + 1)]
}
If you consider both entries in each row at a time, you'll have to also account for the fact that the middle element appears in both diagonals which makes the code unnecessarily messy.
why you are defining the variable product inside the loop,thats why the previous data stored in the variable is lost when it goes out of scope.
#include <iostream>
using namespace std;
double product = 1; // var product should be defined out of the loop
for (int row = 0; row < 4; row++)
{
for (int column = 0; column < 4; column++)
{
if (row==column || row == 3 - column)
{
product *= arr[row][column];
}
}
}
cout << product << ".";
Hi guys I have a 2D array of char '-' in it and I want to print it out.
However I want to print it in a grid format so that the point (0,0) is on the bottom left of the array not the top left.
For example now it's printing:
0 1 2 3
1
2
3
and I want it to print in the way:
3
2
1
0 1 2 3
How can I print my array in order to act more like a graph than an array?
Thanks.
EDIT: This is what my printing function looks like
displayGrid(){
for(int column = 0; column<gridSize; column++){
for(int row = 0; row < gridSize; row++){
std::cout<<gridArray[row][column]<< " ";
}
std::cout<<std::endl;
}
}
This is how I fill the elements in my array:
createGrid(){
int gridSize = 50;
for(int column = 0; column<gridSize; column++){
for(int row=0; row<gridSize; row++){
gridArray[row][column] = empty;
}
}
//gridArray[0][1] = full; first value is x, second is y. Works.
}
Just let your first loop start at gridSize and decrement it.
Should look similar to this:
displayGrid(){
for(int column = gridSize; column>=0; column--){
for(int row = 0; row < gridSize; row++){
std::cout<<gridArray[row][column]<< " ";
}
std::cout<<std::endl;
}
}
I have to display this number pattern:
-3 1 5 9
7 12 17
15 21
using:
int number = __________________; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++)
{
// adjust number so it displays the correct first value in the row
________________________________________________
for (col = 1; col <= ___5 - row___; col++)
{
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
______________________number + row + 3;_________________________________
} / /end inner for loop
cout << endl;
} // end outer for loop
I know number + row + 3 gets the correct numbers across but I cannot seem to get the correct start value.
Try this
number = -3
for(row = 1; row <= 3; row++){
for (col = 0; col < 5 - row; col ++){
display (number + col * (3 + row));
}
number += 12 - row * 2;
}
With your restrictions, that would be:
int number = 0; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++)
{
// adjust number so it displays the correct first value in the row
number += 12 - (row-1) * 2 - (row+2) * (6 - row);
for (col = 1; col <= 5 - row; col++)
{
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
number = number + row + 3;
} / /end inner for loop
cout << endl;
} // end outer for loop
Explanation:
(not a bullet) + [12 - (row-1)*2] is for moving between -3, 7 and 15
(not a bullet) - [ ((row-1) + 2) * (5 - (row - 1))] is for compensating growth of the number in the internal loop
Since the result of the right part with row = 1 is -3, initial number is chosen as 0.
int number = -1; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++) {
// adjust number so it displays the correct first value in the row
number -=2;
for (col = 1; col <= ___5 - row___; col++){
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
______________________number + row + 3;
} / /end inner for loop
cout << endl;
} // end outer for loop
#include<iostream>
using namespace std;
int main()
{
int i,j;
int temp=-3;
for(i=0;i<3;++i)
{
for(j=0;j<4-i;++j)
{
cout<<temp<<" ";
temp=temp+4+i;
}
temp=temp-(4+i);
temp=temp-2;
cout<<"\n";
}
char dd;
cin>>dd;
return 2;
}
Ok so i really think I'm over thinking this, but I've been programming for 48 hours straight and my mind is gone. Background info: using Visual Studio 2010, creating a console application using C++. I was having trouble with some logic, and got stuck on the following:
I have a 2 dimensional 8 x 8 array, matrix[8][8]. This array contains either 0's, 1's, and 2's. Now as the program executes the array replaces either the 1's or 2's with 0's. So as it is running, I want to have a check to see if the array has removed either all of the 1's or 2's. So if there are only 0's and 1's I want to cout a message saying something like "your array no longer contains 2's" and vice versa if there are only 0's and 2's.
Here is some code i was thinking of using:
for(row = 0; row < 8; row++)
{
for (col = 0; col < 8; col++)
{
if(matrix[row][col] != 1){
cout<<"message"<<endl; }
else if(matrix[row][col] != 2){
cout<<"message"<<endl; }
}
}
Now my problem with this is that if the array contains [0, 1, 2, 0] it would run through and check the first element, and it wouldnt contain a 1 or a 2. Some ideas as to what i could do please?
You cannot decide that inside the loop. You only know the result after going through the whole matrix:
bool hasOnes = false;
bool hasTwos = false;
for(row = 0; row < 8; row++)
{
for (col = 0; col < 8; col++)
{
if(matrix[row][col] == 1) {
hasOnes = true;
} else if(matrix[row][col] == 2){
hasTwos = true;
}
}
if (hasOnes && !hasTwos)
cout << "You have removed twos" << endl;
if (hasTwos && !hasOnes)
cout << "You have removed ones" << endl;
I'd suggest you iterate over the matrix and count all elements like this:
int counts[3];
for(i = 0; i < 3; i++)
{
counts[i] = 0;
}
for(row = 0; row < 8; row++)
{
for (col = 0; col < 8; col++)
{
counts[matrix[row][col]]++;
}
}
Once you have this, you can simply check for counts[1] to see the number of ones in the matrix. Also, if you are iteratively replacing the values, you can avoid re-checking the full matrix after each iteration. You can change the counts as you change the matrix.
Assuming you will only have 0-2 in each array element:
int vals = 0;
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
{
vals |= matrix[row][col];
if (vals == 3)
break;
}
if (vals & 1) cout << "Matrix contains 1's" << endl;
if (vals & 2) cout << "Matrix contains 2's" << endl;
int count[3] = {0,0,0};
for (int i = 0; i < 64; i++) count[*(matrix + i)]++;
if (count[0] + count[1] == 64) cout<<"No 2s exist"<<endl;
if (count[0] + count[2] == 64) cout<<"No 1s exist"<<endl;
Edit
This may be slightly faster
int count[3] = {0,0,0};
for (int i = 0; i < 64; i++)
if (!count[1] && !count[2]) count[*(matrix + i)]++;
else break;
if (count[0] + count[1] == 64) cout<<"No 2s exist"<<endl;
if (count[0] + count[2] == 64) cout<<"No 1s exist"<<endl;