Sum of odd numbers between a certain range

I'm trying to write a program that outputs the odd numbers' sum between a certain range. For example, [3,9].

For example,
INPUT:
2
1
5
3
9
OUTPUT:
Case 1: 9 //(1+3+5)
Case 2: 24 //(3+5+7+9)
My code is below.

I encounter a problem that the output contains a really weird and large number which is not my wished output. Please help me correct my code. Thank you all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int a,b[404],c[404];
scanf("%d",&a);
for(int i=1;i<=a*2;i++){
scanf("%d",&b[i-1]);
}
for(int i=b[0];i<=b[a-1];i+=2){
if(i%2==0) c[0]+=i+1;
else c[0]+=i;
}
for(int i=b[a];i<=b[a*2-1];i+=2){
if(i%2==0) c[1]+=i+1;
else c[1]+=i;
}
for(int i=1;i<=a;i++){
printf("Case %d: %d\n",i,c[i-1]);
}
system("pause");
return 0;
}
Last edited on
c[] is uninitialized. On line 11/12 and 15/16 you add some values to a random number.

You set only the fields 0 and 1, hence on line 19 if a > 1 you will get some random numbers
Topic archived. No new replies allowed.