473,408 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

C: Implementing a series

Write a C program to find the sum of the
following
1*1*1+(1*1*1+2*2*2)+(1*1*1+2*2*2+3*3*3)+…n
terms
using for, while, do...while

reply to my inbox pleasee..thank you..:(
i've tried but still didnt work


Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3. int i=1,sum=1,c=1,j;
  4. scanf("%d", &j);
  5. while(i<=j)
  6. {
  7. printf("%d\n", sum);
  8. i=i+1;
  9. c=(c*c*c)+i;
  10. sum=sum+c;
  11. }
  12. }
can anyone check for me the mistake i made??? please..
my output should be
10
9
36
Apr 19 '10 #1
15 1775
Dheeraj Joshi
1,123 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. for(i=1;i<=n;i++)
  2. {
  3.         for(j=i;j>=1;j--)
  4.         {
  5.                 interSum = (j*j*j);
  6.                 sum = sum + interSum;
  7.         }
  8. }
  9. printf("%d\n",sum);
  10.  

Try this.

Regards
Dheeraj Joshi
Apr 19 '10 #2
i followed what you have done. but i've got error like this:

C:\Users\user\Documents\Documents\Downloads\Docume nts\output\acd.c(1): warning #2099: Missing type specifier; assuming 'int'.
C:\Users\user\Documents\Documents\Downloads\Docume nts\output\acd.c(5): error #2048: Undeclared identifier 'n'.
C:\Users\user\Documents\Documents\Downloads\Docume nts\output\acd.c(9): error #2048: Undeclared identifier 'i'.
C:\Users\user\Documents\Documents\Downloads\Docume nts\output\acd.c(14): warning #2027: Missing prototype for 'printf'.
Apr 19 '10 #3
Banfa
9,065 Expert Mod 8TB
@ashiela, what dheerajjoshim provided is not a complete program it as a coding of the algorithm design for you to use with-in the frame work you have already created. However I do not believe it is coded correctly, you should work out in your head or using a calculator or using a spread sheet the result of the algorithm for a few know points, for example the value when n = 1, 2, 3 and 10 so that when you test your program you can run some basic checks to test it is is working.

@dheerajjoshim if it weren't for the fact that I do not believe you have posted a correct implementation of the algorithm required I would say that you were dangerously close to providing the solution to the OP which we discourage here in favour of helping the understand the problem and how to solve it so they can create there own solution.
Apr 19 '10 #4
jkmyoung
2,057 Expert 2GB
Have you ever tried using debugging statements?
eg, in between each round you could put in a statement like so:
printf("i = %d and c = %d \n", i,c);
If those values aren't what you're expecting, you should fix them first.


You can comment out these lines in the final implementation.
Apr 19 '10 #5
i've got my answer thanks anyway :D
Apr 19 '10 #6
donbock
2,426 Expert 2GB
Write an expression (on paper) for the ith term of the sequence. Part of this is deciding if terms go from 0 to n-1; or 1 to n; or 0 to n. Use a calculator or spreadsheet (as suggested by Banfa) to verify this expression. Once you have it on paper, it should be easy to implement in code.

Computing the sum of the terms is trivial:
Expand|Select|Wrap|Line Numbers
  1. sum = 0;
  2. for (i=1; i<=n; i++)
  3.    sum += term(i);
Adjust the starting and ending i accordingly if your terms don't go from 1 to n.
FYI: this code snippet presents a concept; don't expect it to compile.

The value of the sequence gets big pretty fast as n increases. You should do a quick check to make sure your integral type won't overflow. Conversely, it would be a very good idea to determine the smallest value of n that causes overflow and reject any value greater than or equal to that limit. You should also reject negative n; and you should reject n==0 if your terms start at 1. I advise you to get in the habit of validity-checking all user input.

Once you get this working you may want to make it run faster: look for a relationship between term(i) and term(i-1). However, don't get ahead of yourself -- you gain nothing by optimizing a program that doesn't work.
Apr 19 '10 #7
whodgson
542 512MB
Seeing the OP didn`t give us his solution I think the following provides.
Expand|Select|Wrap|Line Numbers
  1. for(int i=1;i<=k;i++)
  2.     {
  3.     n+=i*i*i;
  4.     }
  5. //n is the sum of k terms
Apr 22 '10 #8
Banfa
9,065 Expert Mod 8TB
No n is the kth term
Apr 22 '10 #9
jkmyoung
2,057 Expert 2GB
you just need a m+=n in the loop.

n = Tk
m = Sk
Apr 22 '10 #10
donbock
2,426 Expert 2GB
... find the sum of the following
1*1*1 + (1*1*1+2*2*2) + (1*1*1+2*2*2+3*3*3) + …
The kth term of the series (Tk) is Sum[j=1:k](j^3).
The sum the OP asked for is Sum[i=1:n](Ti).
That is, ...
... Sum[i=1:n](Sum[j=1:i](j^3))
Apr 22 '10 #11
whodgson
542 512MB
@Banfa I don`t think so.....'n+=' results in a running total.
Apr 23 '10 #12
Banfa
9,065 Expert Mod 8TB
n is a running total but it is the running total for the current term not the running total for the actual result of the series.
Apr 23 '10 #13
whodgson
542 512MB
Yes my 12th thread is garbage as you say Banfa it is the kth term. now all I have to do is find my missing neurones
Apr 24 '10 #14
Banfa
9,065 Expert Mod 8TB
I think I saw them bouncing on the floor and rolling under the skirting board :D
Apr 24 '10 #15
thanks everyone i've got it :)
Apr 24 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
70
by: Rajan | last post by:
Hi, I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) { dest = malloc(bytes); } Now if I want to copy the bytes from src to dest, how do I copy these...
6
by: Kerry Sanders | last post by:
I am working on a project for work where I need a specialized type converter to convert the value of a string which is edited in a grid back to the underlying object type from the cell. The value...
6
by: David J Rose | last post by:
I am having trouble allowing a user to drag an item, whilst also allowing them to click the item. I am using mousedown, mousemove and mouseup. It works if I click the button carefully, but if there...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
3
by: suresh A N | last post by:
Hi All, Can anybody help me for these programs. I need to submit ny assignments. 1) Write a C++ program to generate Lucas series 2) Write a C++ program to accept the student's information...
4
by: keirnus | last post by:
Hello, I've been coding in Access VBA to create an Excel Graph and it was good. Until I got this error: Please check the code below: Private Sub TestGraph3()
21
by: puzzlecracker | last post by:
Problem: I send a lot of requests to the application (running on a different box, of course), and I receive back responses from the app . Below: socket corresponds to Socket socket=new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.