Search

Blogging through my thoughts

Felipe GM : ¡Be welcome to my personal and academical blog!

Tag

WSQ04

WSQ04 – On to functions – Sum of numbers.

¡Hello , programmers and curious friends! This is my fourth program , created using Ubuntu bash and Atom. For this task , my classmates and I were instructed to create a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

I decided to use a loop to calculate the sum, because my programming teacher Ken Bauer, wanted us to practice repetitive work.

_______________________________________________________________

The resources that helped me were:
-How to think like a computer scientist.

-cpluplus.com

-Programming hub, mobile app.

I added some extra steps, because I wanted to show more information to the users , like:

-Both numbers are the same. Give me a correct range.

– The sum is also executed if the user writes the upper and lower bound in the wrong order.

WSQ04ATOM1

WSQ04ATOM2
Atom code.
WSQ04UBUNTU
Ubuntu commands.

WSQ04MASTERY

Until next time!

WSQ 04- Sum of numbers.

¡Hello , programmer and curious friends! This is my fourth program , created using Cygwin and Atom. For this task , my classmates and I were instructed to create a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

I decided to use a loop to calculate the sum, because my programming teacher Ken Bauer, wanted us to practice repetitive work.

The resources that helped me were:
-How to think like a computer scientist.

-cpluplus.com

-Programming hub, mobile app.

I added some extra steps, because I wanted to show more information to the users , like:

-Both numbers are the same. Give me a correct range.

– The sum is also executed if the user writes the upper and lower bound in the wrong order.

Here you will find my code and some captures:

wsq04-cygwin
Running on Cygwin.
wsq04-code
Atom code.

#include
using namespace std;

int sum(int min,int max);

int main(){
cout << “This program prints the sum of numbers in a range(inclusive):” << endl;
int a,b;
cout << “Enter the lower bound:” << endl; cin >> a;
cout << “Enter the upper bound:” << endl; cin >> b;

if (a<b){
cout << “The sum from ” << a << ” to ” << b << ” is: ” << sum (a,b) << endl; } else if (a>b){
cout << “The sum from ” << a << ” to ” << b << ” is: ” << sum (b,a) << endl;
}
else {
cout << “Both numbers are the same. Give me a correct range.” << endl;
}
return 0;
}

int sum (int base , int limit)
{
int sum=0;
for (int i = base ; i <= limit; i++){
sum +=i;
}
return sum;
}

wsq03-topics
Mastery topics involved.

wsq04-topic

masterytopics1
Mastery topics involved.

Until next time.

°Luis Felipe Garate Moreno.

Powered by WordPress.com.

Up ↑