473,383 Members | 1,829 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,383 software developers and data experts.

I have a problem to solve summation in c++

Hi , Every one I have a problem to solve this summation in c++ ;
1/3 + 3/5 +5/7 .......

this the program I wrote
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <math.h>
  5.  
  6. main()
  7. {
  8.   int i=3.0,j=1.0,d,N;
  9.   cout<<"Enter Number ";
  10.   cin>>N;
  11.   float sum;
  12.      while(j<=N && i<=N){
  13.   for (d=1; d <= N; d++){
  14.     sum += j/i;
  15.     printf("%d:  %2.2f\n", d, sum);
  16.     i+=2;
  17.     j+=2;
  18.   }}
  19.   getch();
  20. }
and this is the run
http://www.el3mlak.net/up/images/6293428.png

is there a problem in my program >


thanks
May 21 '07 #1
6 2788
Savage
1,764 Expert 1GB
Hi , Every one I have a problem to solve this summation in c++ ;
1/3 + 3/5 +5/7 .......

this the program I wrote
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <math.h>
  5.  
  6. main()
  7. {
  8.   int i=3.0,j=1.0,d,N;
  9.   cout<<"Enter Number ";
  10.   cin>>N;
  11.   float sum;
  12.      while(j<=N && i<=N){
  13.   for (d=1; d <= N; d++){
  14.     sum += j/i;
  15.     printf("%d:  %2.2f\n", d, sum);
  16.     i+=2;
  17.     j+=2;
  18.   }}
  19.   getch();
  20. }
and this is the run
http://www.el3mlak.net/up/images/6293428.png

is there a problem in my program >


thanks
A few problems:

1.u don't initialize sum to 0.0;

2.while loop is not needed here,a single for should solve it

3.please improve ur tabbing style(ur style makes code difficlut to read.

Savage
May 21 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
If you are coding in C++, you should be using the accumulates algorithm instead of re-coding it as you have done. There are no bugs in accumulates whereas your code needs work and when finally working will have to maintained thereby increasing the overall software costs.
May 22 '07 #3
JosAH
11,448 Expert 8TB
If you are coding in C++, you should be using the accumulates algorithm instead of re-coding it as you have done. There are no bugs in accumulates whereas your code needs work and when finally working will have to maintained thereby increasing the overall software costs.
This is all true but my guess is that the OP is beginning to study C++ and most
certainly, the STL hasn't been part of the subjects for study yet. Blame it on the
courseware instead if you like but in defense of the OP: they need to crawl first
before they can walk or run or even *appreciate* the power of the STL. And then
it's still anothert step to be able to *use* all that power.

kind regards,

Jos
May 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Yes, it's a paradigm shift to start C++ with the STL and end it with low-level stuff like arrays and such. Your courseware was correct for the time in history that it was prepared.
May 22 '07 #5
JosAH
11,448 Expert 8TB
Yes, it's a paradigm shift to start C++ with the STL and end it with low-level stuff like arrays and such. Your courseware was correct for the time in history that it was prepared.
minor nitpick: that wasn't *my* courseware ;-)

kind regards,

Jos
May 22 '07 #6
Savage
1,764 Expert 1GB
minor nitpick: that wasn't *my* courseware ;-)

kind regards,

Jos
Jos,seriously,ur username should be:

Jos The Nitpicker

Savage
May 22 '07 #7

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

Similar topics

27
by: Alf P. Steinbach | last post by:
I'm writing a little piece on pointers, to be "bundled" with my attempted correct C++ tutorial (see FAQ item 29.20, yes, I've finally started to at least think about that again), and while thinking...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
1
by: crazydrummer | last post by:
hi everyone, fairly new to access and VBA, but have fundamental knowledge. as part of a college project i am building a database that records "downtime" for machines in a company. the...
7
abouddan
by: abouddan | last post by:
Hi all I am using Access2000 and I am creating a report based on a query. Query1=" select Id,name,Amount from Eployees" This query returns many records, so in the report this result must...
25
by: Darsin | last post by:
Hi all I need to perform a summation on a column of a table based on a criteria given in another column in the same table. The catch is i need to perform different sums according to the number of...
7
by: akmaRudiliyn | last post by:
Hai everybody :). I have problem and need help. ERROR: ERROR Violation of PRIMARY KEY constraint 'PK_Table1_01'. Cannot insert duplicate key in object 'dbo.table1'. ERROR The statement has...
32
by: =?ISO-8859-1?Q?Szabolcs_Horv=E1t?= | last post by:
I did the following calculation: Generated a list of a million random numbers between 0 and 1, constructed a new list by subtracting the mean value from each number, and then calculated the mean...
2
by: galaxianape | last post by:
Please help me with summation in C lang. I m trying to divide the summation of alpha * delta with summation of alpha this is half the program that i have written...but it gives error. pls help.....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.