Printing an Array in rows and columns( one dimensional array) - c++

I need to print an int array that has 50 values the array must contain multiple rows and each row can't have any more than 15 variables on it. Can any one point me in the right direction or explain where I'm going wrong?
#include <iostream>
using namespace std;
int main()
{
int alhpa[51];
for(int i = 0; i < 51; i++)
{
alpha[i] = -1; // initializes all elements to -1
}
for(int i = 0; i < 51; i++)
{
for(int j = 0; j < 15; j++)
{
cout << alpha[j] << "\t";
}
cout << endl;
}
return 0;
}
So the above code works it just doesn't do what I intended it to, if you run this you'll see that the array is printed in rows and each row does have 15 variables on it. How ever the issue is that it only prints the first 15 elements in the array and each time the main loop executes the process is reset and as I said above the only output is the first 15 variables over and over again.
I need to figure out how to print this array out in the same way it's printing now but I need all the elements in the array to be processed and printed.
I know that the last line wouldn't have 15 on it because there should only be room for 5 because 15 * 3 = 45
Any help would be awesome!

How about modifying this code
for(int i = 0; i < 51; i++)
{
for(int j = 0; j < 15; j++)
{
cout << alpha[j] << "\t";
}
cout << endl;
}
to this
for(int i = 0; i < 51; i += 15)
{
for(int j = i; j < 15+i && j < 51; j++)
{
cout << alpha[j] << "\t";
}
cout << endl;
}
This prints them in sets of 15 each line
The output of your program in this case will be
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1

Change
cout << alpha[j] << "\t";
to
if(15*i+j < 51)
cout << alpha[15*i+j] << "\t";
If the element you access is only j, which loops from 0 to 14, it is normal that you only print the first 15 values.
With this change you can also change the outer loop to for(int i = 0; i < 4; i++)

As an alternative, you can use a counter for elements per line:
int count = 0;
for (int i = 0; i < 51; i++)
{
cout << alpha[i] << "\t";
if (++count == 15)
{
cout << endl;
count = 0;
}
}
if (count > 0) // EOL after any residual values
cout << endl;

Your
cout << alpha[j] << "\t";
Only references the j var, meaning it will just repeat the same first 15 items in the array over and over. You will need to refactor this nested loop to achieve the results you want.

Related

Checking how many times a number has been entered

I'm trying to solve one of the questions on a task sheet I've received, to help me further in my understanding of C++ code from my class.
It kept showing 100000 in output after I entered the values. Where is that 1 coming from?
I know there are better ways to write code for this but I just want to know were is my problem.
The question is
(and I quote):
Write a program that:
Asks the user to enter 10 numbers between 1 and 5 into an array and displays the array on screen.
Creates a second array of size 5 and fills it with zeros.
Counts how many 1s, 2s, , … 5s have been entered into the first array and stores this number in the second array.
Displays the second array as shown in the example below.
Code:
int A1[10];
int A2[5] = { 0,0,0,0,0 };
int count = 10;
for (int i = 0; i < count; i++)
{
here:
cout << endl << i + 1 << "- enter a number between 1 and 5 for value : ";
cin >> A1[i];
if (A1[i] < 1 || A1[i]>5)
{
cout << "eror! enter a number between 1 and 5!";
goto here;
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 1; j < 6; j++)
{
if (A1[i] = j)
{
A2[j - 1]++;
break;
}
}
}
for (int i = 0; i < 5; i++)
cout << A2[i];
The error is on the row 21 or 22, you are using a single = which is the assignment sign, inside the if statement, so you are overwriting the value of A[i] to the value of j, but want to check if the element of A[i] is equal to j... So you have to add a = in the if statement.
I don't recommend that you use goto:, it creates spaghetti code. you can put an i-- in your error clause, like so:
int temp;
for (int i = 0; i < count; i++){
cout << i + 1 << "- enter a number between 1 and 5 for value : " << endl;
cin >> temp;
if (temp >= 1 && temp <=5)
A1[i] = temp;
else
i--;
}
Also, if you want to compare 2 values, you should use the == operator. that is what's causing the problem in your second loop
like so:
for(int i = 0; i < count; i++){
for(int j = 1; j < 6; j++)
if(A1[i] == j)
A2[j-1]++;
}
This should work.

Can't find path in Minimum Cost Path using Dynamic Programming

The algorithm is supposed to find the minimum cost path in NxN matrix given as an input. The starting cell is always left bottom and the destination is right top.
Each cell of the matrix represents a cost to traverse through that cell.
You can only move up and right.
I have managed to find the cost, however, I still struggle to backtrack the path.
I tried to start from top right cell and use the greedy algorithm to find my "way back", but the output was either completely wrong or skipping random columns/rows. I also tried to keep track of decisions I was making by creating an additional matrix, but I always end up stuck in the loop.
So how do I find the path?
Here's the code that works well (counts the cost and that's it):
#include <iostream>
using namespace std;
int main()
{
int tab[101][101], N, cost[101][101], backtrack[101][101];
cout << "N (size of NxN matrix) :" << endl;
cin >> N;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cin >> tab[i][j];
cost[i][j] = 0;
backtrack[i][j] = 0;
}
}
cost[N-1][0] = tab[N-1][0];
int a = N-1;
for(int i = N-2; i >= 0; i--) // column 0 can be chosen only in 1 way
{
cost[i][0] = cost[i+1][0] + tab[i][0];
backtrack[i][0] = 4; // came from down
}
for(int j = 1; j < N; j++) // row N-1 can be chosen only in 1 way
{
cost[a][j] = cost[a][j-1] + tab[a][j];
backtrack[a][j] = 3; // came from right
}
for(int i = N-2; i >= 0; i--)
{
for(int j = 1; j < N; j++)
{
if(cost[i][j-1] <= cost[i+1][j])
{
cost[i][j] = tab[i][j] + cost[i][j-1];
backtrack[i][j] = 3;
}
else
{
cost[i][j] = tab[i][j]+cost[i+1][j];
backtrack[i][j] = 4;
}
}
}
cout << "Cost: " << cost[0][a] << endl;
return 0;
}
Now, here's the function with flawed additional matrix that's supposed to give me the path, but ends up in an infinite loop:
(matrix backtrack from previous code was given as track here)
void path(int track[101][101], int N)
{
int help[101][101];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
help[i][j] = 0;
}
int w = 0, k = N-1;
help[w][k] = 1; // top right argument is included in the output
while(w < N || k >= 0)
{
if(track[w][k] == 3)
{
help[w][k-1] = 1; // 3 means I came from the previous column k-1
k--;
}
else if(track[w][k] == 4)
{
help[w+1][k] = 1; //4 means I came from the previous row w+1
w++;
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(help[i][j] != 0)
cout << i << " " << j << endl;
}
}
}
Example input:
5
2 3 4 2 5
5 2 1 2 2
2 4 2 2 3
1 2 2 4 3
3 2 1 2 3
Expected output:
Cost: 20
4 0
4 1
4 2
3 2
2 2
1 2
1 3
0 3
0 4
Actual output
Cost: 20
And no path at all since it ends up in an infinite loop.
You have written the while loop in path() incorrectly:
while(w < N || k >= 0)
...
You intend this loop to continue until w = N-1 and k=0, which it does, but the loop doesn't terminate there, it just runs in place. (You could see this yourself by adding cout << w << " " << k << endl; to the loop.) The conditional I think you want is:
while(w < N-1 || k > 0)

How does c++ react to int array indexes while performing standard mathematical expressions?

The idea I have is pretty simple. What I'd like to do is just have the user input a series of integers and plug those integers specified into an array. So for example, here's a segment of the program I'm writing.
const int size = 1000;
int array_one[size], array_two[size];
for (int i = 0; i < size; i++)
{
std::cin >> array_one[i];
if (array_one[i] == -1) break;
}
for (int i = 0; i < size; i++)
{
std::cin >> array_two[i];
if (array_two[i] == -1) break;
}
The cut off point is -1. Should the user enter this, one index of the array will be all integers greater than -1 with the following indexes housing 0's in this 1000 sized array.
What I'm doing next is simply replicating how you would write out basic addition on paper. So, lets say for example I plugged in 1 0 0 6 -1 as inputs for the first array and then 2 3 4 for the second array. The whole entire output should look something akin to this
1006
+ 234
------
1240
Here's what I've typed out to get this sort of output
int array_size_one = 0;
int array_size_two = 0;
int space_size = 3;
int cut_off_space = 0;
int count = 0;
for (int i = 0; i < size; ++i)
{
if (array_one[i] < 0) break;
array_size_one += 1;
}
for (int i = 0; i < size; ++i)
{
if (array_two[i] < 0) break;
array_size_two += 1;
}
if (array_size_one > array_size_two) space_size += array_size_one;
else if (array_size_two > array_size_one) space_size += array_size_two;
if (array_size_one < array_size_two) cut_off_space += array_size_one;
else if (array_size_two < array_size_one) cut_off_space += array_size_two;
std::cout << std::setw(space_size - array_size_one);
for (int j = 0; j < size; ++j)
{
if (array_one[j] < 0) break;
std::cout << array_one[j];
}
std::cout << "\n+";
std::cout << std::setw((space_size - 1) - array_size_two);
for (int j = 0; j < size; ++j)
{
if (array_two[j] < 0) break;
std::cout << array_two[j];
}
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
This is the part where I think I'm sure I'm having trouble with.
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
The idea was that I wanted to add up everything inside each index of the arrays to perform standard integer expressions. The for loop above isn't really cutting it. I'm getting outputs like 5 or 15. If going by the example above that has inputs of 1 0 0 6 -1 for the first array and then 2 3 4 -1 for the second array. What could I actually do inside the very last for loop iteration to make sure I get the correct outputs replicating standard integer expressions?

How to print the index of the first element of a sequence of the longest repeating number?

I am writing a program for an assignment, which is:
Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found
in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
const int SIZE = 20;
int main()
int arr[SIZE];
int numToss;
srand(time(0));
cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
cin >> numToss;
while (numToss > SIZE || numToss < 1)
{
cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
cin >> numToss;
}
for (int i = 0; i < numToss; i++)
{
arr[i] = 1 + rand() % 6;
cout << " " << arr[i];
}
cout << endl;
int max_count = 0;
int length = arr[0];
for (int i = 0; i < length; i++)
{
int count = 1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > max_count)
max_count = count;
}
int result = 0;
for (int i = 0; i < length; i++)
{
int count=1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count == max_count)
{
max_count = arr[i];
result = i;
cout << "The longest run occurs at " << result << endl;
}
}
return 0;
It does not print correctly, how ca i fix this? Any help is appreciated!

For loop with multiple variable

Is it possible for a for loop with 2 variables to stop incrementing only one of the variable when a condition is met ? For example
for(int i = 0, j = 0; i < 5 && j < 10; i++, j++)
{
cout << i << " " << j << endl;
}
and the output would look something like
0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9
This is my actual code. I wanted the condition for both variables
cout << sp.dets.size() << " " << gt.groundtruth.size() << endl;
for (int i = 0, j = 0; i < sp.dets.size() && j < gt.groundtruth.size(); j < gt.groundtruth.size() ? j++ : j, i < sp.dets.size() ? i++ : i)
{
cout << i << " " << j << endl;
}
sp.dets.size = 0
gt.groundtruth.size() = 8
It would be nice if the solution works for any number i.e. i > j or i < j or i = 0 or j = 0
You can use ternary statement to increment variable i value. Like below:
for(int i = 0, j = 0; i < 5 && j < 10; j++, i<4? i++: i)
{
cout << i << " " << j << endl;
}
This will output the expected result.
Something like this may work,
int k = 0;
int val = 4;
for(int i = 0, j = 0; i < 10 && j < 10; i++, j++){
if (i>=val){
k=val;
cout << k << " "<< j << endl;
}
else
cout << i << " " << j << endl;
}
Prints this,
0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9
Don't try to be overly clever by trying to fit all the logic in one line. It't more important that your code can be read by others (and yourself in two weeks' time) than saving a single line. If the incrementing logic of the two variables is more complex than just increasing by one for each loop, put them in seperate lines, like this:
while (i<5 && j<10) {
...
// complicated expression calculating new i
// complicated expression calculating new j
}
In your case, you might also flip the logic around:
for (int i=0; i<max(sp.dets.size(), gt.groundtruth.size(); i++) {
cout << min(i, sp.dets.size()-1)
<< ","
<< min(i, gt.groundtruth.size()-1)
<< endl;
}
You can use ternary operator on both variables. Just be sure to change your condition to break only when both reach their destined value
for (int i = 0, j = 0;
i != 5 || j != 10;)
{
i += i < 5 ? 1 : 0;
j += j < 10 ? 1 : 0;
}
You'll note I moved the increment into the loop body, this improves readability, in my opinion.