I am trying to store and find the sum of the first 20 odd numbers from 0 to 100 but each time i run the program i get a black screen. Thanks for your time.
#include <iostream>
usingnamespace std;
int oddSum(int a[]);//prototyping functions
void print (int a[]);
int main()
{
int odd[20];//array to sotre first 20 odd numbers
for(int i = 0; i < 100; i++)//lop to cyclye thjorugh all odd numbers from 0 to 100
{
do{//do while loop to store only the first 20 odd numbers
if(i % 2 !=0)
{cin>>odd[i];}//store in the array
} while (odd[i]<20);
}
print(odd);//display the result from the void function
return 0;
}
int oddSum(int a[])//function to find the sum
{
int sum =0;
for(int i = 0; i < 20; i++)
{sum = sum + a[i];}
return (sum);
}
void print (int a[])//function to display the results
{
cout<<"The sum of the first 20 odd numbers from 0 to 100 is:"<<oddSum(a);
}
#include <iostream>
usingnamespace std;
int oddSum(int a[]);//prototyping functions
void print (int a[]);
int main()
{
int odd[20];//array to sotre first 20 odd numbers
for(int i = 0; i < 100; i++)//lop to cyclye thjorugh all odd numbers from 0 to 100
{
do{//do while loop to store only the first 20 odd numbers
if(i % 2 !=0)
{odd[i]=i;}//store in the array
} while (odd[i]<20);
}
print(odd);//display the result from the void function
return 0;
}
int oddSum(int a[])//function to find the sum
{
int sum =0;
for(int i = 0; i < 20; i++)
{sum = sum + a[i];}
return (sum);
}
void print (int a[])//function to display the results
{
cout<<"The sum of the first 20 odd numbers from 0 to 100 is:"<<oddSum(a);
}
for(int i = 0; i < 100; i++)//lop to cyclye thjorugh all odd numbers from 0 to 100
{
do{//do while loop to store only the first 20 odd numbers
if(i % 2 !=0){
odd[i]=i;//store in the array
}
}while (odd[i]<20);
}
#include <iostream>
usingnamespace std;
int oddSum(int a[]);//prototyping functions
void print (int a[]);
int main()
{
int odd[100];//array to store odd numbers
for(int i = 0; i < 100; i++)//loop to count to 100
{
if(i % 2 !=0)//if statement to store odd numbers if it is odd
{odd[i]=i;}//store in the array
}
print(odd);//display the result from the void function
return 0;
}
int oddSum(int a[])//function to find the sum
{
int sum =0;
for(int i = 0; i < 20; i++)
{sum = sum + a[i];}
return (sum);
}
void print (int a[])//function to display the results
{
cout<<"The sum of the first 20 odd numbers from 0 to 100 is:"<<oddSum(a);
}
You only store odd numbers at odd indices in your odd array. The rest of the elements have unspecified contents. So, if you print the first 20 elements of your odd array, you (would) print a bunch of junk.
[Edit: And there is no real reason to store the odd numbers:]
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
int main()
{
unsigned oddsTotal = 0 ;
for ( unsigned oddsFound = 0; oddsFound < 20; ++oddsFound )
oddsTotal += oddsFound*2 + 1 ;
std::cout << "The sum of the first 20 odd numbers is: " << oddsTotal << '\n' ;
}
int main()
{
int odd[100];
int j = 0;
for(int i = 0; i < 100; i++)
{
if(i % 2 !=0)
{
odd[j]=i;
cout << i << endl;
if(j == 20)
break;
j++;
}
}
print(odd);//display the result from the void function
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
cout << "Enter the number of odd numbers to be summated from 1\n : ";
int limit;
cin >> limit;
int oddSum = 0;
int oddnum = 1;
vector<int> odds;
odds.reserve(20);
odds.push_back(oddnum);
for(int i = 0; i < limit; i++)
{
oddSum+=oddnum;
oddnum+=2;
odds.push_back(oddnum);
}
cout << oddSum;
return 0;
}