I'm beginner in c++, and I want to make a program that delete a row from a matrix.. Like, If I say 3 3 2
1 2 3 it need to show 1 2 3
4 5 6 7 8 9
7 8 9
The program works like this: 3 = rows, 3 = columns, 2 = the deleted row. "3 3 2" is not a row...
I wrote this :
#include <iostream>
using namespace std;
int main() {
int N, M, v[100][100];
cin>>N>>M;
int i,j,p;
cin>>p;
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j)
cin>>v[i][j];
for (i = 1; i <= N; ++i) {
for (j = 1; j <= M; ++j)
cout<<v[i][j]<<" ";
cout<<"\n";
}
for (i = p; i < N; ++i)
v[i][j]=v[i+1][j];
--N;
for (i = 1; i <= N; ++i){
for (j = 1; j <= N; ++j)
cout<<v[i][j]<<' ';
cout<<"\n";
}
return 0;
}
But it doesn't work.... Can someone help me?
modified code (it will work fine ):
#include <iostream>
using namespace std;
int main()
{
int N, M, v[100][100];
cin>>N>>M;
int i,j,p;
cin>>p;
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j)
cin>>v[i][j];
for (i = 1; i <= N; ++i)
{
for (j = 1; j <= M; ++j)
cout<<v[i][j]<<" ";
cout<<"\n";
}
for (i = p; i < N; ++i)
for(j=1; j<=M; ++j)
v[i][j]=v[i+1][j];
--N;
for (i = 1; i <= N; ++i)
{
for (j = 1; j <= M; ++j)
cout<<v[i][j]<<' ';
cout<<"\n";
}
return 0;
}
This doesn't work, what do you think j is doing in this code?
for (i = p; i < N; ++i)
v[i][j]=v[i+1][j];
--N;
You need to loop over rows and columns. Copy each column in every row greater that the row you want to delete. In other words you need nested loops here
for (i = p; i < N; ++i)
for (j = 1; j <= M; ++j)
v[i][j] = v[i+1][j];
--N;
You should use vector
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//matrix
vector< vector<int> > V;
//To Add
for(int i=0; i<100; i++)
{
vector<int> R;
for(int j=0; j<100; j++)
{
int x;
cin>>x;
R.push_back(x);
}
V.push_back(R);
}
//To delete a row
int row_to_delete = 2;
V.erase(V.begin() + row_to_delete);
//To access
for(int i=0; i<V.size(); i++)
{
for(int j=0; j<V[i].size(); j++)
{
cout<<V[i][j];
}
}
}
vector<T> is class that use a template to create a dynamic array.
vector<int> is a var array int. V.push_back(T) you can add a element to array with type T, and with V.erase(V.begin() + int ) you can delete a element to array in this case a row. With V.size() you can take a elements count inside the array.
Related
I'm trying to find the smallest of the biggest sum of each column of every possible permutations of a given 2D array NxN, where the values in each row can shift towards the left. For example, the array
4 6
3 7
would have 4 possibles permutations:
4 6 6 4 4 6 6 4
3 7 3 7 7 3 7 3
The biggest sum of each permutation is respectively, 13, 11, 11, 13. Thus the smallest of the biggest sums is 11. I have written a recursive function that should work, but for some reason, it only works for arrays that are smaller than 6x6... I'm new at programming, and just recently learned about recursion, any help or counsel on how to think recursively and to debug code would be greatly appreciated...
For the array 4x4
7410 1371 2665 3195
4775 4130 6499 3414
300 2092 4009 7638
5351 210 7225 7207
The answer is 18349, and my code gives me the correct answer.
However, for the array 6x6
5219 842 7793 2098 5109 2621
1372 3253 3804 5652 810 1620
4894 6792 1784 4335 4772 6656
3203 1070 4716 5335 1157 6855
5529 2767 2205 408 7516 7454
375 7036 2597 5288 937 2893
The answer should be 23733, but I've got 24176. How is this possible?
Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Ok I believe I understand my mistake, I have to reset the matrix to its original state at the end of each base case, when the matrices are small, the code is still capable of finding all the possible biggest sums, but when the matrices got bigger, some of the possibilities weren't generated. Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], OrigMatrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = OrigMatrix[i][j];
}
}
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
OrigMatrix[i][j] = matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
when I tried to multiple two negative numbers the value it is zero in c++,
for example -5 * -3
the result is zero,
why?
this is my code
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void Multiply(const int v_arr[], const int m_arr[][3], int signed
o_arr[], int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
o_arr[i] = 0;
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
}
//End your code here
}
int main()
{
int n;
cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
cin >> v_array[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> m_array[i][j];
}
}
//fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
cout << o_array[j] << " ";
}
return 0;
}
how to fix it to get the correct result?
the input is
2
2 -3
2 -3
2 -4
Your issue is here:
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.
Assuming this is matrix*vector multiplication code, it should look like this (untested):
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
Also, your loop based on j is useless. You can remove it:
void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
for(int k = 0; k < 3; k++) { //initialize output array
o_arr[k] = 0;
}
for (int i = 0; i < size; i++) {
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
}
Remove the rows where a value is encountered twice from the matrix. Where's the bug in my code that I've already written, so that I do it without using any function or anything that would make it easier?
My code:
#include <iostream>
using namespace std;
int main()
{
int v[99][99], m, n, i, j, k, vlinie[99], a=0;
cout<<"m="; cin>>m;
cout<<"n="; cin>>n;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
cin>>v[i][j];
//finding out each row that has doubles and remembering its index in an array called vlinie
for(i=1; i<=m; i++)
for(j=1; j<=n-1; j++)
for(k=j+1; k<=n; k++)
if(v[i][j]==v[i][k])
{
a++; //a is the number of numbers in vlinie
vlinie[a]=i;
}
//removing duplicates from vlinie, in case there are any
for(i=1; i<=a-1; i++)
for(j=i+1; j<=a; j++)
if(vlinie[i]==vlinie[j])
for(k=i; k<=m; k++)
{
vlinie[k]=vlinie[k+1];
a--;
}
//this is where we move the rows around, and supposedly the line where i got it wrong, so what do i change in this line?
for(i=1; i<=a; i++)
for(j=1; j<=n; j++)
for(k=vlinie[i]; k<=m; k++)
v[vlinie[i]+k][j]=v[vlinie[i]+k+1][j];
for(i=1; i<=m; i++)
{
cout<<endl;
for(j=1; j<=n; j++)
cout<<v[i][j]<<" ";
}
return 0;
}
Example:
1 2 2 3
4 5 6 7
7 7 8 9
7 6 5 4
What it should output:
4 5 6 7
7 6 5 4
0 0 0 0
0 0 0 0 (without the zeros theoretically but the way i wrote it leaves it like this, for now)
What it outputs instead:
1 2 2 3
7 7 8 9
5 4 3 2 (it didn't delete the lines that it should delete and instead deleted some line it should keep)
How do I fix this?
PS: yes, i know i shouldn't have started my indexing at 1 instead of 0, no need to remind me about it again
Your problem was at these lines :
for(i=1; i<=a; i++)
for(j=1; j<=n; j++)
for(k=vlinie[i]; k<=m; k++)
v[vlinie[i]+k][j]=v[vlinie[i]+k+1][j];
There is a first issue with the indices: we should copy v[k+1][] to v[k][].
The second issue is that once a first move has been performed, the table vlinie is no longer valid.
This last point is not so easy to correct.
Moreover, for each suppressed row, you are implementing a shift of several rows, which leads to a poor efficiency.
It is simpler and more efficient to memorize which rows are suppressed (suppress[]) and from this table, to determine which row of the input matrix will end at the ith row of the final matrix (table posi[]).
Hereafter is an example of implementation.
#include <iostream>
int main()
{
int v[99][99], m, n, i, j, k, posi[99];
bool suppress[99];
std::cout << "m = "; std::cin >> m;
std::cout << "n = "; std::cin >> n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
std::cin >> v[i][j];
//finding out each row that has doubles
for (i = 0; i < m; i++) {
suppress[i] = false;
for (j = 0; (j < n-1) && !suppress[i]; j++)
for (k = j+1; (k < n) && !suppress[i]; k++)
suppress[i] = (v[i][j] == v[i][k]);
}
// determination of the moves to be performed
int n_keep = 0;
for (i = 0; i < m; i++) {
if (!suppress[i])
posi[n_keep++] = i;
}
//this is where we move the rows around
for (i = 0; i < n_keep; i++)
for (j = 0; j < n; j++)
v[i][j] = v[posi[i]][j];
// Set to 0 remaining rows
for (i = n_keep; i < m; i++)
for (j = 0; j < n; j++)
v[i][j] = 0;
for(i = 0; i < m; i++)
{
std::cout << "\n";
for(j = 0; j < n; j++)
std::cout << v[i][j] << " ";
}
return 0;
}
I have written some code that is meant to sort an array of strings using the radix sort, starting with the least significant digit. This function assumes all of the strings are the same length and each character is lowercase.
I am encountering crashes whenever I get to the loop in which I assign values to the temporary array. You can see my function here:
#ifndef RADIX_H
#define RADIX_H
#include <string>
#include <iostream>
using namespace std;
void lsd_string_radix(string array[], int array_size, int max_chars)
{
string *temp = new string[array_size];
for(int i = max_chars - 1; i >= 0; i--)
{
int count[26] = {0};
for(int j = 0; j < array_size; j++)
{
count[static_cast<int>(array[j][i]) - 97]++;
}
for(int j = 1; j <= 26; j++)
{
count[j] += count[j - 1];
}
for(int j = 0; j < array_size; j++)
{
temp[count[static_cast<int>(array[j][i])]++] = array[j]; // crashes here
}
for(int j = 0; j < array_size; j++)
{
array[j] = temp[j];
}
}
}
#endif
I'm guessing I have a failing in logic but I can't figure it out for the life of me.
After the second loop, count[0] should be zero, and the third loop is missing a -97. This example fixes the problem using count of size 27 instead of 26. The first loop in this example uses -96, so count[0] = 0, count[1] = # instances of 'a's, count[2] = # instances of 'b's, ... . count[26] = # instances of 'z's but it's only used in the first loop. It's not needed, but it's simpler to put a count of 'z's there rather than adding an if statement to avoid storing a count at count[26].
#include<iomanip>
#include<iostream>
#include <string>
using namespace std;
void lsd_string_radix(string array[], int array_size, int max_chars)
{
string *temp = new string[array_size];
for(int i = max_chars - 1; i >= 0; i--)
{
int count[27] = {0};
for(int j = 0; j < array_size; j++)
count[static_cast<int>(array[j][i]) - 96]++;
for(int j = 2; j < 26; j++)
count[j] += count[j - 1];
for(int j = 0; j < array_size; j++)
temp[count[static_cast<int>(array[j][i]) - 97]++] = array[j];
for(int j = 0; j < array_size; j++)
array[j] = temp[j];
}
}
int main()
{
string a[6] = {"mnop", "ijkl", "efgh", "uvwx", "qrst", "abcd"};
lsd_string_radix(a, 6, 4);
for(size_t i = 0; i < 6; i++)
cout << a[i] << endl;
return 0;
}
If the size of count[] is to be 26, the first loop needs to be modified:
for(int j = 0; j < array_size; j++){
if(array[j][i] == 'z')continue;
count[static_cast<int>(array[j][i]) - 96]++;
}
or the first two loops are modified:
for(int j = 0; j < array_size; j++)
count[static_cast<int>(array[j][i]) - 97]++;
int m = 0;
int n;
for(int j = 0; j < 26; j++){
n = count[j];
count[j] = m;
m += n;
}
I cannot understand/think of a case where my code fails to give correct output.
Link to the problem: http://www.spoj.pl/problems/MKBUDGET/
The problem clearly has a DP solution. I am posting my solution below:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<vector <int> > opt;
void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
for(int i = A[0]; i <= max_a; i++) //for num workers in 1st month
opt[0][i] = i*(hire + sal);
for(int i = 1; i < n; i++) //num of months
for(int j = A[i]; j <= max_a; j++) //num of workers for ith month >=A[i] and <= max_a
{
opt[i][j] = opt[i-1][A[i-1]] + j*sal + (A[i] > A[i-1] ? (A[i]-A[i-1])*hire : (A[i-1] - A[i])*fire);
for(int k = A[i-1]; k <= max_a; k++)
opt[i][j] = min(opt[i][j], opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
}
}
int ans(vector<int> A, int n, int max_a)
{
int ret = opt[n-1][A[n-1]];
for(int i = A[n-1]; i <= max_a; i++)
ret = min (ret, opt[n-1][i]);
return ret;
}
int main()
{
vector<int> A;
int n, hire, fire, sal,max_a, c = 1;
while(1)
{
cin >> n;
if(n == 0)
break;
A.clear();
opt.clear();
max_a = 0;
cin >> hire >> sal >> fire;
A.resize(n);
for(int i = 0; i < n; i++)
{cin >> A[i];
max_a = max(max_a,A[i]);
}
opt.resize(n);
for(int i = 0; i < n; i++)
opt[i].resize(max_a + 2);
compute_opt(A,n,hire,fire,sal,max_a);
cout << "Case " << c << ", cost = $" << ans(A,n,max_a) << endl;
c++;
}
return 0;
}
I am getting correct answers for the two sample test cases but I get a WA when I submit. Any help ?
OK, so your problem is that you disallow the case where you hire any number of employees between A[i] and A[i - 1]. Maybe it's a good idea to fire some unneeded employees, but not all. That's why you get WA. I modified your code and got it accepted:
void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
// Fill all disallowed entries with infinity
for (int i = 0; i < A[0]; ++i)
opt[0][i] = 1000000000;
for(int i = A[0]; i <= max_a; i++) //for num workers in 1st month
opt[0][i] = i*(hire + sal);
for(int i = 1; i < n; i++)
for(int j = 0; j <= max_a; j++)
{
// No need for special case handling,
//just check all previous numbers of employees
opt[i][j] = 1000000000;
if (A[i] > j) continue;
for(int k = 0; k <= max_a; k++)
opt[i][j] = min(opt[i][j],
opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
}
}
By the way, there's a "greedier" solution than the one you have that does not depend on the number of employees being small (so that the table can be allocated).