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]
Related
This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed last month.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int size; char str[size]; cin>>size;
for(int i=0; i<size; i++)
{
cin>>str[i];
}
return 0;
}
example: size=10;
I was expecting this program to take 10 inputs but it is taking severral inputs.
if it has something to do with cin then please explain.
I fixed it. Try it.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int size;
cin>>size;
char str[size];
for(int i=0; i<size; i++)
{
cin>>str[i];
}
return 0;
}
It hasn't anything to do with the cin, to put 10 inputs you need to change the line 7 to "for (int i=0; i<<size; i++)" I haven't tried it yet but maybe it would solve your problem
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.
This question already has answers here:
The program doesn't give any output on VS Code if I use 'const int ' and assign it a big value like 1e6 [duplicate]
(1 answer)
Why is this C++ program crashing? [duplicate]
(2 answers)
C++ program is not giving output involving large array
(5 answers)
Closed 9 months ago.
Whenever is give N = 1e6 + 2 like large value to N. VS Code doesn't work and on runtime shows .exe stopped working. Couldn't figure out why happening
#include <iostream>
using namespace std;
int main()
{
int n, arr[n];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int N = 1e6 + 2;
bool check[N];
for (int i = 0; i < N; i++)
{
check[i] = false;
}
return 0;
}
This question already has answers here:
Passing a 2D array to a C++ function
(17 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
Closed 3 months ago.
I am trying to pass a 2D array to a function but I am failing to do so. There is no problem with passing 1D array. How can I do this?
#include <iostream>
using namespace std;
void DisplayBoard(int matrix[],int n) // Prints out the given array.
{
for (int j = 0; j < n; j++)
{
cout << matrix[j];
}
}
int main()
{
int n,m;
cin>>n;
//int matrix[n]={};
DisplayBoard(matrix,n);
return 0;
}
i think you want to input index number (n) from user and then input array object from user too
i complete your code like this :
#include <iostream>
using namespace std;
void DisplayBoard(int matrix[],int n) // Prints out the given array.
{
cout<<endl ;
for (int j = 0; j < n; j++)
{
cout << matrix[j]<<" ";
}
}
int main() {
int n,m;
cin>>n ;
int matrix[n];
for(int i=0;i<n;i++) {
cin>>matrix[i];
}
DisplayBoard(matrix,n);
return 0;
}
remember to declare array in main function too ,
that was one of your code error !
and this is incorrect to declare array :
int matrix[n]={} // incorrect !
Well. int matrix[n]={}; fills it with zeros and that's what I wanted to do. And my code with 1D array works fine but if I do it like so it does not.
#include <iostream>
using namespace std;
void DisplayMatrix(int matrix[][],int n,int m)
{
for(int i=0;i<n;i++)
{
for (int j = 0; j < m; j++)
{
cout << matrix[j];
}
}
}
int main()
{
int n,m;
cin>>n>>m;
int matrix[n][m]={};
DisplayMatrix(matrix,n,m);
return 0;
}
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I have tried to implement a simple function which returns the pointer to first element in array.
#include<iostream>
using namespace std;
int * func(int n)
{
int arr[n];
for (int a = 0; a <n; a++)
{
arr[a]=a;
}
return arr;
}
int main()
{
int n;
cin>>n;
int * arr=func(n);
for (int i = 0; i <n; i++)
{
cout<<*(arr+i)<<endl;
}
}
I have assumed that array occupies contiguous block of memory,then why the output of this program isn't what expected.
if n=10 then output is
0
-1216233191
-1215824576
-1215824576
-1215855028
-1215820256
-1074779464
-1215820256
-1074779464
134514382
int * func(int n)
{
int arr[n]; <<<<<<<<<<<<<<<local variable to this function.
for (int a = 0; a <n; a++)
{
arr[a]=a;
}
return arr;
}
You should not return address of local variables...