what is wrong with the logic that i am using? - c++

theres a logic error , for the code i m trying to do ,
*
**
***
**
*
so this is the pattern , what my code is running is this
*
**
***
**
**
**
there seems to be logic error for the printing of stars , i just wanted to know what logic should i use.
heres the code :-
#include<iostream>
using namespace std;
int main()
{
/*
*
**
***
**
*
*/
int i,rows;
cout<<"Enter number of rows :"<<endl;
cin>>rows;
for(i = 1; i <= ((rows/2)+1) ; i++)
{
for(int j = (rows - i); j >= 1; j--)
{
cout<<" ";
}
for(int k = 1; k <= i; k++)
{
cout<<"*";
}
cout<<endl;
}
for(i = ((rows/2)+1) ; i <= rows; i++)
{
for(int j = 1; j <= i; j++)
{
cout<<" ";
}
for(int k = (rows/2); k >= 1; k--)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}

You can change your second loop as follows:
for ( i = ( rows / 2 ); i >= 1; i-- ) {
for ( int j = ( rows - i ); j >= 1; j-- ) {
cout << " ";
}
for ( int k = 1; k <= i; k++ ) {
cout << "*";
}
cout << endl;
}
It reuses the logic from the first loop, but just changes it to go from rows/2 down to 1 (inclusive) instead.
The first loop header also runs too many times for even values. For example, with 4, it does 4 / 2 + 1 which is 3. What you want is below.
for ( i = 1; i <= (int)( rows / 2. + .5 ); i++ )
This effectively rounds. So for 4 you now have 4 / 2. + .5 which is 2.5, then converted to int is 2. With 5, you get 5 / 2. + .5 which is 2.5 + .5 which is 3.0, then to an int is 3

You don't need to initialize i in the second for loop. Just let continue with the next row value after the first loop finishes.
The number of * you print is controlled by the k loop. In the first row loop you print out i of them, while in the second you always print out the same number - rows / 2. You'll want to print out a gradually reducing number of them - rows + 1 - i. Or just use for (int k = i; k <= rows; ++k).

Related

Subarray with given sum

Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case is N and S, where N is the size of array and S is the sum. The second line of each test case contains N space separated integers denoting the array elements.
Output:
For each testcase, in a new line, print the starting and ending positions(1 indexing) of first such occuring subarray from the left if sum equals to subarray, else print -1.
Constraints:
1 <= T <= 100
1 <= N <= 107
1 <= Ai <= 1010
Example:
Input:
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output:
2 4
1 5
My Code:
#include <iostream>
using namespace std;
int main() {
int t, n, s, a[1000], result[1000];
cin >> t;
for (int i = 0; i < t; i++) {
result[i * 2] = -1;
cin >> n >> s;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
int flag = 0;
for (int j = 0; j < n; j++) {
if (flag == 0) {
int sum = 0;
for (int k = j; k < n && sum < s; k++) {
sum += a[k];
if (sum == s) {
result[i * 2] = j + 1;
result[(i * 2) + 1] = k + 1;
flag = 1;
break;
}
}
}
}
}
for (int i = 0; i < t * 2; i += 2) {
if (result[i] != -1) {
cout << result[i] << " " << result[i + 1] << endl;
} else {
cout << result[i];
}
}
}
Result:
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
4 225
9 45 10 190
Its Correct output is:
-1
And Your Code's output is:
-1-1-1-138 42
I've just found this:
https://www.youtube.com/watch?v=G0ocgTgW464
However I believe that the time complexity is O(n*log(n)) given the fact that map::find is O(log(n))

Finding largest value in every array's column

Currently I have a 2D array and I need to find largest value of every array's column and find their sum.
My code seems to not work properly because I'm getting largest values of every row, not column, also the program sometimes prints the same value two times.
I wrote this code based on a tutorial and I don't know where I made a mistake. There should be basic C++ functions (cycles, if statements, arrays).
My code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, m, a[10][10], sum = 0, max;
cin >> n >> m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[j][i];
}
}
for (int i = 0; i < m; i++) {
max = a[0][i];
for (int j = 0; j < n; j = j + 1) {
if (a[j][i] >= max) {
max = a[j][i];
sum = sum + a[j][i];
cout << max << endl; //control printing to see the values
}
}
}
cout << sum;
}
Input:
3 3
3 2 3
7 5 1
6 3 5
Output I need to get is 17 but I am getting 19 right now.
Assuming your array has m columns and n rows.
int sum = 0, max = 0;
for( int i = 0 ; i < m ; ++i )
{
max = 0;
for( int j = 0 ; j < n ; ++j )
{
if(max < a [j][i])
{
max = a[j][i];
}
}
sum += max;
}

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)

Output not correct for my C++ program

So, I have the following problem:
From the file tabl.in a number n will be read (n<=50).
After that a square array with n rows and n columns will be read; all the numbers in the array will be composed by a maximum of 2 digits each.
Shown in the file tabl.out, the modulo between the sum of numbers found on the second diagonal of the array and 10, if the sum is palindrome (true=1, false=0), and the arithmetic mean of elements situated below of the main diagonal.
Will be writing functions for:
reading the array
calculation of the operation sum of secondary diagonal%10
checking if the previous result it is palindrome
calculation of the arithmetic mean below main diagonal
Example:
tabl.in:
4
5 8 2 12
1 0 3 16
1 2 1 11
5 7 2 19
tabl.out:
2 1 3
where
(12+3+2+5)%10 = 22%10 = 2
22 is palindrome = 1
1+2+2+1+7+5 = 18, 18/6=3
My code so far is:
#include <fstream>
using namespace std;
ifstream fin("tabl.in");
ofstream fout("tabl.out");
void readn(int Arr[][51], int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
fin >> Arr[i][j];
}
int sumsec(int Arr[][51], int n) {
int s = 0;
float r;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
r = s % 10;
return r;
}
void pald(int Arr[][51], int n) {
int s = 0, pal = 0;
for (int i = 1; i < n; i++)
s = s + Arr[i][n - i + 1];
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == s)
fout << "1 ";
else
fout << "0 ";
}
int ambmd(int Arr[][51], int n) {
int s = 0, k;
float ame;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
s = s + Arr[i][j];
k++;
}
}
ame = s / k;
return ame;
}
int main() {
int Arr[51][51], n;
float r, ame;
fin >> n;
readn(Arr, n);
r = sumsec(Arr, n);
fout << r << " ";
pald(Arr, n);
ame = ambmd(Arr, n);
fout << ame;
}
But I have an issue with the palindrome() function: my output file will have 2 0 3 written to it for the given array from the example, instead of 2 1 3. What am I doing wrong?
Your pald function would work, if you compute s the same way as you do in sumsec and if s would still contain the sum, after you compute pal.
In your case, while (s != 0) {...}, followed by if (pal == s) {...} could be re-written as if (pal == 0), which is clearly not the intended solution. Just save your sum before computing pal, then compare with the saved sum.
Also, change your loop condition for computing s to for (int i = 1; i <= n; i++).
int s = 0, pal = 0, sum = 0;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
sum = s;
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == sum)
fout << "1 ";
else
fout << "0 ";
You should also consider the various comments for code improvements, like not re-computing the sum in the pald function.

Traversing a multi array with pointers

for ( j = 0; j < d1; j++ ){
m += j;
for ( i = 0; i < d1*d2; i +=d2){
cout << *(m+i);
}
cout << endl;
}
d1,d2 are array dimensions
and
int* m = new int [d1*d2];
I want to traverse over my array and simply group and print the columns.Can't figure what's wrong with this code.Seems to be working fine untill the 3rd iteration in the following example:
Let's say my input values are 1 2 3 4 5 6 7 8 9
I get:
1 4 7
2 5 8
4 7 (something random)
In
m += j;
you are first incrementing m by 0, then by one, then by 2. If we originally took a copy
int *start = m;
then in the first iteration of the outer loop, we'd have
m == start
in the second,
m == start + 1
in the third
m == start + 3
You'd want m == start + 2 there. Except that you want to keep m in order to delete it at the end, so you shouldn't change m at all but use something like
for ( j = 0; j < d2; j++ ){
for ( i = j; i < d1*d2; i +=d2){
cout << *(m+i);
}
cout << endl;
}
m = &a[0];
for ( j = 0; j < d1; j++ )
{
for ( i = 0; i < d2; i++)
cout << *m++;
cout << endl;
}