This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 7 months ago.
The counter does not increase at all so I want to know which part went wrong.
#include <cstring>
#include <iostream>
using namespace std;
int main(){
int total=0,na=0,ni=0;//use to count how much type of element is there
string chem;
getline(cin,chem);
for(int i=0;i!='\0';i++){
if(chem[i]=='N'){
if(chem[i+1]=='i'){
ni++;
}else{
na++;
cout<<na<<endl;
}
}
}
na and total values are both 0
cout<<na<<endl;
total=total + ni+na;
cout<<total;
for(int i=0;i!='\0';i++){
is the same as
for(int i=0; i; i++){
('\0' is a char type with value 0).
The conditional check means the loop body never runs.
Related
This question already has answers here:
C/C++ the result of the uninitialized array
(2 answers)
Uninitialized variable behaviour in C++
(4 answers)
Closed last month.
i want to create a new array that include negative numbers from array t. My code is running,ı have no error but I get only 00000
#include "iostream"
using namespace std;
int main()
{
int t[3][12]={{-2,7,6,13},{48,-5,1,-7},{16,-9,2,-9}};
int i,j,k;
k=0;
int a[12];
for(i=0; i<=2; i++)
{
for(j=0; j<=3; j++)
{
if(t[i][j]<0)
{
t[i][j]=a[k];
k++;
cout<<t[i][j];
}
}
}
cout<<endl;
}
the error is in t[i][j]=a[k], it should be a[k] = t[i][j]
This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 2 months ago.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void converter(vector<char>keypad[],int num[], int index, string result, int size_of_num){
if(index == size_of_num){
cout << result << " ";
return;
}
int digit = num[index];
int size_of_keypad = keypad[digit].size();
for(int i=0; i < size_of_keypad; i++){
converter(keypad, num, index + 1, result + keypad[digit][i], size_of_num);
}
}
int main(){
vector <char> keypad[] = {
{},{},{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
{'j','k','l'},
{'m','n','o'},
{'p','q','r','s'},
{'t','u','v'},
{'w','x','y','z'}};
int numbers[] = {2,3,4};
int size_of_num = sizeof(numbers);
converter(keypad, numbers, 0, string (""), size_of_num );
return 0;
}
I tried running this code, which was to transfrom phone numbers into words. I used vector in this problem with a reucrsive function. But I am not sure if its just the memory overflowing or just totally something else.
I tried and search online in order to solve this and I found the correct code, but everything they were doing was the same and the only thing that was differnt were the varible names. I just couldn't figure out whats wrong.
you calculate size wrong
int size_of_num = sizeof(numbers);
should be
int size_of_num = sizeof(numbers)/ sizeof(numbers[0])
or even
int size_of_num = std::size(numbers);
This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
Closed 3 months ago.
Input is same for both platforms
8 5
10 9 8 7 7 7 5 5
Codeforces
VS Code
what is this happening?
is there something wrong in my code?
Code--->
#include <iostream>
using namespace std;
int main(){
int n;
int a[n];
int k;
cin>>n>>k;
int p=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]>0){
if(a[i]>=a[k-1]){
p++;
}
}
}
cout<<p;
return 0;
}
what do you think this does
int n;
int a[n];
n is some random value, you then use that to size your array. You must read n first
Note that VLA (variable length arrays) are not standard c++. Use std::vector instead
This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Closed 1 year ago.
https://www.hackerrank.com/challenges/variable-sized-arrays/problem
This is the problem statement.
I tried the following code
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,q;
cin>>n>>q;
int n1;
int A[n][1000000];
for(int i =0; i<n; i++)
{ cin>>n1;
for(int j=0; j<n1; j++){
int c;
cin>>c;
A[i][j] = c;
}
}
int a,b;
for(int i=0;i<q; i++){
cin>>a>>b;
cout<<A[a][b]<<"\n";
}
return 0;
}
This code passes the sample test case and other custom inputs(I have tried for small number of input values). But it does not work for Test cases in which the value of n and q (as mentioned in the problem) are large. It gives the "Segmented Fault" error. Can someone please explain why I am getting that error.
Over here int A[n][1000000]; your allocating a n * 1000000 number of elements. That is a huge number. If we consider an int to be 32 bits (or 4 bytes), your talking about n * 4000000 bytes worth of data, which will most probably will be in the range of Megabytes and hence your error. This is easily too much to be stack allocated, and greatly inefficient.
Consider using std::vector<> instead.
This question already has answers here:
std::vector::resize() vs. std::vector::reserve()
(5 answers)
Closed 6 years ago.
I need to fill a vector in with particular values. I find that the below code works, except in that a.size() fails to change from 0. Adding a resize call after I put in the elements takes almost twice as long. It seems there should be an O(1) way to update the size, since I don't need to change any elements. Is there? Should I just not bother?
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
int n = 1e9;
vector<float> a;
a.reserve(n);
for (int i=0; i<n; i++)
a[i] = i;
cout << a[2]; //successfully prints as 1
cout << a.size(); //confusingly prints as 0
}
Edit: this is not a duplicate, as the linked question does not address benchmarking. It simply asks the difference in reserve and resize, which I am not asking about. This code works, and fast, but has the ugly side effect of leaving size() "incorrect."
std::vector::reserve() is not for creating elements. To create elements, you can use std::vector::resize().
Try this:
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
int n = 1e9;
vector<float> a;
a.resize(n);
for (int i=0; i<n; i++)
a[i] = i;
cout << a[2];
cout << a.size();
}