answersLogoWhite

0


Best Answer

The simplest way is to store the sequence in an array, then pass the array to the following function:

int sum (int* a, int size) {

int s = 0;

for (int i=0; i<size; ++i) s += a[i];

return s;

}

Example usage:

int x[5] = {1, 2, 3, 4, 5};

int s = sum (x, 5);

assert (s == 15);

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program that sums a sequence of integers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a loop that sums the odd numbers from 1 to 100 in Java format?

Like this: int sum = 0; for(int i = 1; i&lt;=100; i+=2){ sum+=i; } The "for" loop starts the counter i at 1, continues while it is less than or equal to 100, and increments it by 2 at a time.


What happens to voltage and current in series circuits?

Kirchoff's voltage law states that the signed sums of the voltage drops in a series circuit add up to zero.Kirchoff's current law states that the current everywhere in a series circuit is the same, more specifically, that the signed sums of the currents entering a node is zero.


What happens when resistors are interconnected in a circuit?

The net resistance can be found out using the algebraic sums f series and parallel connections. When there is no current flowing in the circuit the net resistance is infinite.


How do you calculate space complexity?

Calculate the amount of additional memory used by the algorithm relative to the number of its inputs. Typically the number of inputs is defined by a container object or data sequence of some type, such as an array. If the amount of memory consumed remains the same regardless of the number of inputs, then the space complexity is constant, denoted O(1) in Big-Omega notation (Big-O). If the amount of memory consumed increases linearly as n increases, then the space complexity is O(n). For example, the algorithm that sums a data sequence has O(1) space complexity because the number of inputs does not affect the amount of additional memory consumed by the accumulator. However, the algorithm which copies a data sequence of n elements has a space complexity of O(n) because the algorithm must allocate n elements to store the copy. Other commonly used complexities include O(n*n) to denote quadratic complexity and O(log n) to denote (binary) logarithmic complexity. Combinations of the two are also permitted, such as O(n log n).


How do you make a vbs script with numpad numbers?

The VBScript/Visual BASIC Script programming language uses both letters/numbers to create programs with. So, I'm not exactly sure what you mean by making a VBScript which uses only numbers, alone...? As the 'numpad' is made up of just numbers. You need to explain yourself a little bit better, I'm afraid. Did you mean, use VBScript to create a 'calculator program' with, possibly? Anyway, here is a simple VBScript calculator program... num1=CInt(InputBox("Enter the first number: ", "PROGRAM: Add 2 numbers")) num2=CInt(InputBox("Enter the second number: ", "PROGRAM: Add 2 numbers")) MsgBox("The answer is: " &amp; num1+num2) Type the above code into Windows Notepad; then, save it as... add2num.vbs ...go and find the file you just saved; and, left double click on it to make the program RUN/execute. When the program runs you should see... 1. Enter the 1st number: (you can use the NumPad to type in a number) 2. Enter the 2nd number: (you can use the NumPad to type in a next number) 3. Finally, you should see a Windows standard message box appear with the sum total result of the two numbers which you did previously type in. To do other sums...then, you can modify this simple calculator using... + ...plus sign - ...minus sign * ...multiplication sign / ...division sign

Related questions

What two consecutive even integers that sums 34?

The integers are 16 and 18.


What are the partial sums of the infinite geometric sequence 2 4 8 16 32 64...?

Partial sums for a sequence are sums of the first one, first two, first three, etc numbers of the sequence. So, the series of partial sums is:2, 6, 14, 30, 62, ...It is the sequence whose nth term isT(n) = 2^(n+1) - 2 for n = 1, 2, 3, ...


How do you calculate hard sums of the topic integers?

Not all people will find the same sums hard. Also, when you are older you may well find that sums that look hard now are really quite easy.


What is every term of a sequence is 1?

It is a valid sequence which is fundamental to arithmetic since its partial sums define the counting numbers.


What is an antimagic square?

An antimagic square is a heterosquare in which the sums form a sequence of consecutive numbers.


What has the author Carlos J Moreno written?

Carlos J. Moreno has written: 'Sums of squares of integers'


What is half of 53?

Half of 53 is 26.5 or 26 and a half.


How do you estimate decimal sums and differences?

The basic idea is the same as when you estimate sums and differences of larger numbers (which may or may not be integers). You round the numbers to one or two decimal digits, then add them up.


Does the square root of pi end?

No. Pi is an irrational number, which means that its value cannot be expressed exactly as a fraction m/n, where m and n are integers. Consequently, its decimal representation never ends or repeats. It is also a transcendental number, which implies, among other things, that no finite sequence of algebraic operations on integers (powers, roots, sums, etc.) can be equal to its value.


What is the difference between an arithmetic series and a geometric series?

An arithmetic series is the sequence of partial sums of an arithmetic sequence. That is, if A = {a, a+d, a+2d, ..., a+(n-1)d, ... } then the terms of the arithmetic series, S(n), are the sums of the first n terms and S(n) = n/2*[2a + (n-1)d]. Arithmetic series can never converge.A geometric series is the sequence of partial sums of a geometric sequence. That is, if G = {a, ar, ar^2, ..., ar^(n-1), ... } then the terms of the geometric series, T(n), are the sums of the first n terms and T(n) = a*(1 - r^n)/(1 - r). If |r| < 1 then T(n) tends to 1/(1 - r) as n tends to infinity.


How do you figure how many digits are in pi?

&pi; is an irrational number, which means that its value cannot be expressed exactly as a fraction m / n, where mand n are integers. Consequently, its decimal representation never ends or repeats. It is also a transcendental number, which implies, among other things, that no finite sequence of algebraic operations on integers (powers, roots, sums, etc.) can be equal to its value.The fraction 22/7 is close but not exactly &pi;.


Find the sum of the integers 1 to 1000?

This kind of question is frequently asked and easily solved. 1 + 1000 = 1001 2 + 999 = 1001 3 + 998 = 1001 You can see the developing pattern here. Taking integers in order from the top and bottom of the list allows you to create hundreds of equal sums. The last such sum that can be made is 500 + 5001 = 1001. It is clear that you then have 500 sums, each equal to 1001. 1001 X 500 = 500,500 which is the sum of all the integers combined.