Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I tried to learn basics for C++. Now I'm trying to figure out the arrays. But then i running this small program my IDE raise the null pointer exception.I didn't understand why this happend.
include <iostream>
using namespace std;
int main(){
int[] arr = {1, 2, 3};
for(int i=0; i < 4; i++){
cout << arr[i] << endl;
}
exit 0;
}
I searched for answers on google about null pointer exception. I understand what it is, but don't understastand why it raised in my program. My array has 3 items, so the i<4 statement is correct in my opinion
The array has three elements, but you are trying to access the fourth one on the last iteration. The condition in the for loop is wrong. (Note: you can calculate the number of elements using sizeof arr / sizeof *arr or std::size(arr), since C++ 17.)
int arr[] = {1, 2, 3};
for (int i = 0; i < 3; i++){
std::cout << arr[i] << '\n';
}
To avoid this sort of issue altogether, use a range-based for loop.
for (const int& x : arr) {
std::cout << x << '\n';
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Side note: I've been trying to trace this code, but every time I end up with output, it seems
to be far more or less than the right answer which is 30. If anybody can explain in to me, I'd be more than appreciated. Thanks
Always make sure to add { and } to loops so that you avoid thinking that some instruction is within a loop when it's actually not.
The code you added in the screenshot is equivalent to the following
#include <iostream>
using namespace std;
int main() {
int t = 0;
for (int c = 0; c < 3; ++c) {
// +8
for (int v = 0; v < 4; ++v) {
for (int h = 0; h < 2; ++h) {
++t;
}
}
// +2
++t;
++t;
}
cout << t << endl;
return 0;
}
So, each iteration of the first loop (variable c) adds 10 to t. There are three iterations of the first loop, the total value added to t is 30.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
now have a char array like
char a[5]={1,2,3,4,5}
but how to add them up?
I try this
int sum=0
for(int i=0;i<4;i++)sum = sum + a[i];
cout<<sum;
but it is wrong.
why?
The loop must go to 5 so that the indexes are 0, 1, 2, 3, 4
Like this:
for(int i=0;i<5;i++)
Besides that you are missing ; in the end of two lines.
So the whole code:
char a[5]={1,2,3,4,5}; // added ;
int sum=0; // added ;
for(int i=0;i<5;i++)sum = sum + a[i]; // Changed 4 to 5
cout<<sum;
will print 15
The issue is that you are not looping enough times and missing the final item. That is already established in another answer.
There are mechanisms in c++ to avoid such out by one errors (one of the most common types).
Take these two examples that do not require to explicitly state how many elements are in the collection you are iterating over.
#include <iterator>
#include <numeric>
#include <iostream>
int main()
{
const char a[5] = {1, 2, 3, 4, 5};
/*
#1 use range based for loop
*/
int t = 0;
for(char c: a)
t += c;
std::cout << t << '\n';
/*
#2 use implicit loop in accumulate algorithm
*/
int i = std::accumulate(std::begin(a), std::end(a), 0);
std::cout << i << '\n';
}
These are two idiomatic ways to write c++ and should eliminate a large number of simple errors from your code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Why does this result run into infinite loop?
After entering the for loop, I printed values of i and v.size()-2 and they are 0 and -1 respectively. Hence, the loop condition must be false. Then, how does this program get into the for loop in the first place?
#include <iostream>
#include <vector>
int main() {
std::vector<int> v {1};
std::cout << "Size:: " << v.size();
for (int i=0 ; i <= (v.size()-2) ; i++) {
std::cout << "Hello";
}
return 0;
}
The problem is in comparing to an unsigned number after an underflow.
The size of the vector is 1. You subtracr 2, and get -1 mathematically. In unsigned math, however, you get a very large number, so your loop continues for much longer than you expected.
To avoid situations like this, replace subtraction with addition:
i+2 <= v.size()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My code is below but there is some error can anyone guide me to write logic to return continuous numbers, for example, if array[] = {1,3,5,2,3,4,7,4,5,6} then function should return 2,3,4,4,5,6 keep time complexity in mind?
#include <stdio.h>
#define max 10
int coll[max];
void call_sort(int* p) {
int i = 0, first, sec;
while (*p) {
first = *p;
p++;
sec = *p - 1;
if (first == sec) {
coll[i] = *p;
i++;
}
int j;
for (j = 0; j < max; j++) {
printf("%d ", coll[j]);
//coll=coll+1;
}
}
}
int main(void) {
printf("ya\n");
int buff[max], i;
for (i = 0; i < max; i++)
scanf("%d", buff[i]);
call_sort(&buff);
}
There are many problems with your code
Wrong parameter to scanf(), you need to pass the address of the variable to modify it inside scanf() like this
scanf("%d", &buff[i]);
and you should also check that scanf() did read the value correctly by checking it's return value.
Wrong parameter to call_sort(), this will not cause any problem in this case, but it's wrong, and this combined with the scanf() issue, means that you did not enable compiler warnings, you should.
The correct way to pass buff is simply
call_sort(buff);
Wrong while (*p) which assumes that 0 is the last element of the array.
You should probably pass the size as a parameter and write a for loop, since you are dealing with numbers and 0 is a number, if it were a text string then it would be ok to exclude 0 from the normal values and use it as a sentinel value which is done in the standard library functions for strings.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to print the even numbers of the first 25 Fibonacci numbers. However, I think I have a problem when using a vector as a parameter for my function below. Do you see what I'm doing wrong?
#include <iostream>
#include <vector>
using namespace std;
int main(){
int j=1, k=1, sum;
vector<int> myvector(25);
for (int i=0; i<25; i++) {
//cout << j << " ";
myvector[i] = j;
sum=j+k;
j=k;
k=sum;
}
findeven(myvector);
system("pause");
}
int findeven (vector<int>){
for (int i = 0, i < 25; i++){
if (vector[i] % 2 == 0){
cout << vector[i];
}
}
else{
}
}
vector<int> is just a type name. You need to name the parameter to be able to use it. You also can't use a type name as variable as you attempt to do in your loop. Fixed code:
int findeven( vector<int> v ) {
if (v[i] % 2 == 0)
cout << v[i];
//...
}
Since you don't change the vector inside the function, it'd be a good idea to pass it by const reference to avoid copying it:
int findeven( const vector<int>& v );
You'll also need to make the function visible before you use it. Right now, it's defined after main function and you'll get an error because you're trying to call it where the compiler hasn't seen its declaration yet. Put it before main (or at least its declaration).