Question

In: Computer Science

Requirements: Code in C++. With given information, write the solution to this problem so that it...

Requirements: Code in C++. With given information, write the solution to this problem so that it is understandable to someone with basic knowledge of C++ (ex: only keep basic libraries, keep coding shortcuts to a minimum). Also leave comments in the code (plz), the logic behind solving this problem if possible, and explanation of what the keys to solving this problem is and how to run test cases to ensure correctness of code.

Problem:

For this problem you will compute various running sums of values for positive integers.

Input

The first line of input contains a single integer P, (1 <=P <=10000), which is the number of data sets that follow. Each data set should be processed identically and independently. Each data set consists of a single line of input. It contains the data set number, K, followed by an integer N, (1 <= N <= 10000).

Output

For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by three space separated integers S1, S2 and S3 such that: S1 = The sum of the first N positive integers. S2 = The sum of the first N odd integers. S3 = The sum of the first N even integers.

Sample Input

3

1 1

2 10

3 1001

Sample Output

1 1 1 2

2 55 100 110

3 501501 1002001 1003002  

A Solution that needs to be rewritten so it can be understood more easily

/*
 * Sum Kind Of Problem
 * Compute running sums of odds, evens and all integers
 * Does it 2 ways... using formulas and loops.
 * Define EASY_WAY for formulas
 * Define HARD_WAY for loops
 * Define both of them to show both results
 */
#include <stdio.h>
#include <stdlib.h>

#define EASY_WAY
#undef LONG_WAY

int main()
{
        int n, i, v, k;
#ifdef LONG_WAY
        int oddsum, evensum, sum, j;
#endif

        scanf("%d", &n);
        for(i = 1; i <= n; i++){
                scanf("%d %d", &(k), &(v));
#ifdef EASY_WAY
                printf("%d %d %d %d\n", i, v*(v+1)/2, v*v, v*(v+1));
#endif
#ifdef LONG_WAY
                oddsum = evensum = sum = 0;
                for(j = 1; j <= v; j++){
                        evensum += j*2;
                        oddsum += j*2 - 1;
                        sum += j;
                }
                printf("%d %d %d %d\n", i, sum, oddsum, evensum);
#endif
        }
        return(0);
}

Solutions

Expert Solution

//IF YOU ARE HAPPY WITH THE ANSWER KINDLY LEAVE A LIKE, ELSE COMMENT TO CLEAR DOUBTS

CODE:

#include <iostream>
#include <stdlib.h>
using namespace std;
#define EASY_WAY
#define LONG_WAY

int main()
{
int n, i, v; //n is used for number of datasets, i for loops, v for assigning data value
int oddsum, evensum, sum, j; //oddsum stores the odd_sum, evensum stores the even_sum, sum for total sum
//j for loops
cin>>n; //entering the number of datasets
int array[n][2]; //creating a 2D array to store the dataset
for(i = 0; i < n; i++) //loop to get the input
cin>>array[i][0]>>array[i][1];
#ifdef EASY_WAY //EASY WAY DEFINED
cout<<"\n\nSHOWING EASY_WAY ->";
for(i = 0; i< n; i++){ //loop to get the values from array of datasets
v = array[i][1];//assigning array[i][1] to v so that it is easy to use the value
cout<<"\n"<<i+1<<"\t"<<v*(v+1)/2<<"\t"<< v*v<<"\t"<< v*(v+1); //used functions
}
#endif
#ifdef LONG_WAY //LONG WAY DEFINED
cout<<"\n\nSHOWING LONG_WAY ->";
for(i = 0; i < n; i++){ //loop to get the values from array of datasets
oddsum = evensum = sum = 0; //initializing all values to 0 for calculating
v = array[i][1]; //assigning array[i][1] to v so that it is easy to use the value
for(j = 1; j <= v; j++){ //second loop to calculate the sum
evensum += j*2;
oddsum += j*2 - 1;
sum += j;
}
cout<<"\n"<<i+1<<"\t"<<sum<<"\t"<<oddsum<<"\t"<<evensum;
}
#endif
return(0);
}

CODE PREVIEW:


Related Solutions

Check a number if the Database. Write MIPS assembly code for the following requirements. Given the...
Check a number if the Database. Write MIPS assembly code for the following requirements. Given the following code for the data segment. .data Database: .word 1,2,3,4,5,6,7,8,9,10 Ask user to type in a random integer number using syscall #5. Check if this number is within the database or not, print out "number found!" if the number was foudn in the database, print out "No such number found in database!" if not.
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation Fn = Fn-1 + Fn-2: 0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13 The sequence would be the numbers in red (0,...
Im asked to edit the C++ code given to me so that the user can enter...
Im asked to edit the C++ code given to me so that the user can enter as many courses as they would like. I've already built the array to house the courses, but Im a tiny bit stuck on writing the user input to the array every time a new line Is made. Code: //CSC 211 Spring 2019 //Program Description: // // Originally From: // CSC 211 Spring 2018 // Dr. Sturm // This program... // #include<iostream> #include<string> #include<fstream> using...
In this program, you are modifying given code so that the class is object-oriented. 2. Write...
In this program, you are modifying given code so that the class is object-oriented. 2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.    3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.    b. The second text file contains the distances between the cities in the...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Using C++ code, write a program to convert a distance d given in inches (in) to...
Using C++ code, write a program to convert a distance d given in inches (in) to centimeters (cm), where d is input by a user via the keyboard and 1in = 2.54cm. Your submission should include screenshots of the execution of the program using the values 1.5, 4 and 6.75.
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
PROBLEM: c++ code You are to write a program to tell you how many months it...
PROBLEM: c++ code You are to write a program to tell you how many months it will take to pay off a loan, as well as the total amount of interest paid over the life of the loan. You have just purchased a stereo system that costs $1000 on the following credit plan: No down payment, an interest rate of 18% per year (and hence 1.5% per month), and monthly payments of $50. The monthly payment of $50 is used...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT