I make some program in C++ which in matrix finds longest horizontal line of 0s.
In first line I input n and m (rows and columns in matrix array a), and later array. Main problem is when I insert first line (all correct with it) the program stops with error -1073741510, line.exe has stopped working.
#include <iostream>
using namespace std;
int main()
{
int n, m;
cin >> n, m;
int a[n][m];
int i,j,k;
for (i=0;i<n;i=i+1){
for(j=0;j<m;j=j+1){
int temp;
cin >> temp;
a[i][j] = temp;
}
}
int max;
for (i=0;i<n;i++){
for(j=0;j<m;j++){
if(a[i][j]==0){
for(k=j+1;k<m;k++){
if(a[i][k]==0){
max++;
}else{break;}
}
}
}
}
cout << max;
return 0;
}
Sorry for big amount of for loops, I don't know better way of solving problem.
To read both numbers n and m use:
cin >> n >> m;
Otherwise, as you have cin >> n, m; it is equivalent with
cin >> n;
m; // This has no effect
For a solution to the correct operation of your algorithm, try this:
int maxZeros = 0;
int lineContainingMaxZeros = 0;
for (i = 0; i < n; i++) {
int countZero = 0;
for (j = 0; j < m; j++) {
if (a[i][j] == 0)
countZero++;
}
if (countZero > maxZeros) {
maxZeros = countZero;
lineContainingMaxZeros = i;
}
}
cout << "Line: " << lineContainingMaxZeros << " containing " << maxZeros << " zeros";
Related
I am stuck with this program so far, and I don't know what else to do.
I just need help with the recursive function so I can do the other modes too, which are multiplication and subtraction.
#include "std_lib_facilities.h"
//this contains all the libraries my university offers.
int sum(vector <int> test, int i){
if (i < 0){
return 0;
}
else {
return test[i] + sum(test, i - 1);
}
}
int main(){
int n;
int x;
int mode;
int result = 0;
cout << "give the size of the vector:\n";
cin >> n;
while (n < 0){
cout << "you can't give negative number!give again:\n";
cin >> n;
}
vector <int> myvector;
for (int i = 0; i <= n-1; i++){
cout << "give numbers to fill the vector:\n";
cin >> x;
myvector.push_back(x);
}
cout << "give 1 for sum 2 for multiplication 3 for substraction\n";
cin >> mode;
while (mode < 1 && mode > 3){
cout << "this mode doesn't exist.give again:\n";
cin >> mode;
}
if (mode == 1){
result = sum(myvector, n);
cout << "result is:" << result;
}
}
I want to get all the indexes starting from backwards to sum them all.
You are accessing outside the bounds of your array.
The simple fix to your issue is to pass n-1 to your sum function.
result=sum(myvector,n - 1);
However, there is no need for you to pass n at all, rather i would suggest you add a function that starts the recursion like this. (this is a rather common pattern for recursion)
int sum(const vector<int>& test, int i);
int sum(const vector<int>& test)
{
if(test.empty())
{
return 0;
}
if(test.size() == 1)
{
return test[0];
}
return sum(test, test.size() - 1);
};
int sum(const vector<int>& test, int i)
{
if(i < 0)
{
return 0;
}
return test[i] + sum(test, i - 1);
};
then call it like sum(myvector);
I dont understand why is this code giving runtime error on input of any test case (size of string is n, and then string is input). Not even the word "CHECK" (in the solve() function) gets printed.. Please help!
The problem link is : problem
#include <bits/stdc++.h>
using namespace std;
int a[1000][26];
int mov(int l, int u, int x)
{
if(l==u)
{
if(a[l][x]-a[l-1][x]==1)
return 1;
else
return 0;
}
int m=(l+u)/2;
return max(mov(l,m,x+1)+a[u][x]-a[m][x],mov(m,u,x+1)+a[m][x]-a[l-1][x]);
}
void solve()
{
int i,k,j,n;
string str;
cin >> n >> str;
cout << "CHECK";// This is not getting printed
for(i=0;i<26;i++)
a[0][i]=0;
for(i=1;i<n+1;i++)
{
for(j=0;j<26;j++)
{
a[i][j]=a[i-1][j];
if(str[i-1]==j+'a')
a[i][j]++;
}
}
k=mov(1,n,0);
cout << n-k << "\n";
}
int main()
{
int t=1;
cin >> t;
while(t--)
solve();
}
Your very first input shows how many input "pairs" will be given. In your case, you have skipped this part and gone into gathering the input "pairs". This is why "CHECK" wont get printed. To resolve this, have a cin to capture the number of input pairs, then iterate through the inputs to get the pair.
void solve()
{
int i, k, j, n;
string str;
memset(a[0], 0, sizeof(int) * 26); // Use this instead.
cin >> n;
cout << "CHECK";
for (i = 1; i < n + 1; i++)
{
cin >> j >> str;
for (j = 0; j < 26; j++)
{
a[i][j] = a[i - 1][j];
if (str[i - 1] == j + 'a')
a[i][j]++;
}
}
k = mov(1, n, 0);
cout << k << "\n";
}
Also, try not to use using namespace std;.
I want to create an array of Bitset .Binary Bitset(example "100","1010",etc)
After that I want to input from user and store in the the Bitset .
I have tried the following line but it says error.
#include<bits/stdc++>
using namespace std;
int main()
{
int n,i;
string bit_string;
cin>>n // size of Bitset array.
bitset<8> brr[n];//
for(i=0;i<n;i++)
{
cin>>bit_string;
brr[i](bit_string);
}
return 0;
}
I want to create n Bitset each of size 8 bits.Where n is given by user.
my input is binary string like.
"110010","001110"
please help
The error ocurrs because you are trying to creat a C-style array using n which is not compile-time constant. It's not possible to creat a C-style array without being n known at compile time.
The following is a good way to do what you want
Creat a std::vector<std::bitset<8>> to hold your bitset<8>s, as follows.
Note that the code ignores the excess of characters in strings iput like "111111110" (makes it "11111111") and treats any character except '1' as if it were '0' and if the input string is less than 8 characters, the code adds zeros by the default of the bitsets
#include <vector>
#include <bitset>
#include <iostream>
int main() {
int n, i;
std::string bit_string;
std::cout << "Enter the size";
std::cin >> n; // size of Bitset array.
std::vector<std::bitset<8>> brr(n);//
for (i = 0; i < n; i++) {
std::cin >> bit_string;
for (int j{}; j < bit_string.size() && j < 8; ++j) {
brr[i][j] = (bit_string[j] == '1') ? 1 : 0;
}
}
//To test
for(auto const& el :brr)
{
for(int i{}; i < 8;)
std::cout << el[i++];
std::cout<<"\n";
}
}
See Why is "using namespace std;" considered bad practice?
and
Why should I not #include <bits/stdc++.h>?
For dynamic count of the objects , Please try vector<> instead of array[]
#include<bits/stdc++>
using namespace std;
int main()
{
int n, i;
string bit_string;
cin >> n; // size of Bitset array.
vector<bitset<8>> arr; //size()=>0
arr.resize(n); //size()=>n
for (i = 0; i < n; i++)
{
cin >> bit_string;
bitset<8>& br = arr[i]; //get the i of n
int maxlen = 8;
if (bit_string.size() <= 8)
maxlen = bit_string.size();
else
cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;
for (int j = 0; j < maxlen; j++)
{
if (bit_string[j] == '1')
br.set(j, true);
}
//cout << endl << br << endl; //output test
}
return 0;
}
If you still want to use array , please try this way
#include<bits/stdc++>
using namespace std;
int main()
{
int n, i;
string bit_string;
cin >> n; // size of Bitset array.
bitset<8>* arr = new bitset<8>[n];
for (i = 0; i < n; i++)
{
cin >> bit_string;
bitset<8>& br = arr[i]; //get the i of n
int maxlen = 8;
if (bit_string.size() <= 8)
maxlen = bit_string.size();
else
cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;
for (int j = 0; j < maxlen; j++)
{
if (bit_string[j] == '1')
br.set(j, true);
}
//cout << endl << br << endl; //output test
}
delete[] arr; //IMPROTAND , delete the array and free memory
return 0;
}
The code runs fine in my compiler but shows runtime error on codeforces. I found out that I should avoid unitialized variables but I think I've already covered that.
#include <iostream>
using namespace std;
int main()
{
int i, n, flag = 1, k = 1;
int hours[n];
int minutes[n];
cin >> n;
for (i = 0; i < n; i++)
{
cin >> hours[i];
cin >> minutes[i];
}
for (i = 0; i < n; i++)
{
if (hours[i] == hours[i + 1] && minutes[i] == minutes[i + 1])
{
flag++;
if (flag > k)
k = flag;
}
else
flag = 1;
}
cout << k;
return 0;
}
The problem lies here
int i, n, flag = 1, k = 1;
int hours[n];
int minutes[n];
You declare variables n, but you don't initialize it, and then you use it to specify an array size. Uninitialized variables of type int contain garbage data, and using it is undefined behavior, whatever happens is unspecified, and you can not rely on it.
If you must create an array of size specified by a user input, you need to allocate at runtime.
int *hours;
int *minutes;
cin >> n;
hours = new int[n];
minutes = new int[n];
// Rest of the code
// Remember to delete them
delete[] minutes;
delete[] hours;
Better yet, use a container provided by the standard library, a std::vector would be a perfect container to use here
#include <vector>
std::vector<int> hours;
std::vector<int> minutes;
cin >> n
hours.resize(n);
minutes.resize(n);
VLAs are not standard C++ but are allowed as an extension. The first problem in your code is that you use the variable n without initialization. This causes undefined behavior according to the standard.
You need to move the call to cin() to above the arrays that use n at declaration. The second problem in your code is that the expression inside the if statement attempts to access an element outside the bounds of your array. This also causes undefined behavior.
Here is a refined version of your code:
#include <iostream>
using namespace std;
int main()
{
int n;
int k = 1;
int flag = 1;
cout << "Enter a value for n:" << endl;
cin >> n;
int *hours = new int[n];
int *minutes = new int[n];
cout << "Enter values for hours:" << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":";
cin >> hours[i];
}
cout << "Enter values for minutes:" << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":";
cin >> minutes[i];
}
// note here: 'i' goes to (n - 1) instead of (n) so as not to be out of range
for (int i = 0; i < n - 1; i++)
{
if ((hours[i] == hours[i + 1]) && (minutes[i] == minutes[i + 1]))
{
flag++;
if (flag > k)
{
k = flag;
}
}
else
{
flag = 1;
}
}
cout << "The value of k is: " << k << endl;
delete hours;
delete minutes;
return 0;
}
Beginner programmer. I have an assignment where I am to output numbers using loops. The input is the size of the output. The first assignment is to output the same #s vertically: For example. Input? 5. Output:
12345
12345
12345
12345
12345
My current code is incorrect but here is what I have:
int main(){
unsigned size;
cout <<"Size: ? ";
cin >>size;
cout <<"Numbers Vertically!" <<endl;
for ( unsigned r = 0; r < size; r++ ){
for ( unsigned c = 0; c < size; c++)
cout <<size;
cout <<endl;
}
cout <<endl;
}
You need to make two changes.
First is to actually read in the input using cin.
Second is to print c+1 in your inner loop instead of size.
Here is the code:
int main() {
unsigned size;
cout <<"Size: ? ";
cin >> size; // Read input size
cout <<"Numbers Vertically!" <<endl;
for ( unsigned r = 0; r < size; r++ ) {
for ( unsigned c = 0; c < size; c++) {
cout << c+1; // Print c+1 instead of size
}
cout <<endl;
}
cout <<endl;
}
Here is a running example
You're missing a 'cin >> size;' instruction.
I prefer this way.
#include <iostream>
using namespace std;
int main(){
unsigned size = 10;
unsigned n = size * size;
unsigned i = 1;
for (unsigned r = 0; r < n; r++ ){
cout << i % (size + 1);
if (i % (size + 1) == size) {
cout <<endl;
i = 1;
}
else {
i++;
}
}
cout <<endl;
return 0;
}