C++/dev c++

Advertisement


Question
Good Day!
I just can't figure out on how I can create a program that will input 20 integers. Output the number of negative values and the sum of positive numbers. I'm just a beginner, and I think this will work with looping, right?

Answer
Hello,

You are right that you would need loops. Well, I have written the code for you. Try to study it. I have used STLs. something not good for beginners but if you feel that is too advanced for you, kindly let me know and I will rewrite the programme using just traditional arrays.

Good luck with your programming.



#include <iostream>
#include <vector.h>

using namespace std;
int main (int argc, char * const argv[])
{
  vector<int>container;
  vector<int>::const_iterator index;
  int i, choice, sum  = 0;
  
  for (i = -5; i<20; i++)
     container.push_back(i);
  
  
  
  cout << "1.sum up all the positive numbers?"<<endl;
  cout<<"2.Find the number of negative numbers?"<<endl;
  
  cin>>choice;
  switch (choice) {
     case 1:
         for(index=container.begin(); index!=container.end(); index++)
         {
         if(*index>0)
         sum += *index;
         
         }
        
        break;
        
     case 2:
        for(index=container.begin(); index!=container.end(); index++)
        {
         if(*index<0)
         ++sum;
        }
        
        break;
        
     default:
        break;
  }
  
  cout<<sum<<endl;
   
       return 0;
}


Hello, i do not have devc++ as my IDE but I use mac xcode. That
Shouldn't be a problem. Try to clean and build the programme again.
It works perfectly fine for me. What error are you getting? What happens
When you run the programme?  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


henry

Expertise

I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.

Experience

I have a 3- year- work experience in software Engineering.

Education/Credentials
I am a bachelor of Engineering graduate

©2016 About.com. All rights reserved.