473,800 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Taking C++, need help with a problem pls

5 New Member
Hello all, im taking a intro to c++ class as part of my design comm, and having issues withthe codelab part (online). First off it doesnt give any help to what you are doing wrong.

the prob is:

Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n counting numbers, and store this value in total . Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total . Use no variables other than n , k , and total . Do NOT modify n .

ive attempted this code:


n=0;
k=1;

while( n=0; n >=1; n<=k)
{
total = total + k;
k++;
}


not sure what im doing wrong,.

pls help thanks
Feb 23 '08 #1
6 4331
whodgson
542 Contributor
You need to write the algorithm in plain English along the following lines before writing the code.
Assume n has been initialized to some value and k and total have been declared.
initialize k=0 and total=0
The while parameter could be (k<n) // because u don`t know the value of n
if n=4 k would loop through 0,1,2,3
The loop body could be:{ total+=(k+1)*(k +1)*(k+1)
k++}
return total //after loop terminated
if n=3, total would = 1*1*1 + 2*2*2 +3*3*3=36 maybe!
I hope this does not contravene the guidelines
Feb 23 '08 #2
billyusa
5 New Member
ok i think im getting it,

k=0
total=0

while (n=4; k < n; k++)
{total=total + (k+1)*(k+1)*(k+ 1)
}
Feb 23 '08 #3
billyusa
5 New Member
ok i think im getting it,

k=0
total=0

while (n=4; k < n; k++)
{total=total + (k+1)*(k+1)*(k+ 1)
}

but it doesnt seem to be accepting it, I really dislike codelabs, ratherr then helping you it just sits there, saying ERROR!.

your help is appreciated thanks again
Feb 23 '08 #4
Simonius
47 New Member
Your while looks a bit weird, what you put between the () belongs after a for.
The following code should do the trick.
But it isn't exactly like I want to, are you sure they didn't include math so you could use the pow function?

code removed, please offer algorithm as opposed to the answer

Note total+=k*k*k is the same as total = total + k*k*k
Feb 23 '08 #5
whodgson
542 Contributor
How can you initialize n with 4 if you don`t know the value of n? You want your loop to continue until it is 1 less than n assuming your counter starts at 0.
Feb 24 '08 #6
Simonius
47 New Member
You don't need to initialise n, that's already done.

@ts: what you put in your while belongs in a for, you should just loop it while k is less then n because that's basicly what you want.
Feb 24 '08 #7

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

Similar topics

13
1858
by: Ideasman | last post by:
Hi I have a made a script that process normals for a flat shaded 3D mesh's. It compares every vert with every other vert to look for verts that can share normals and It takes ages. I'm not asking anyone to rewrite the script- just have a look for any stupid errors that might be sucking up time.
0
411
by: Kumar | last post by:
Need urgent help..... I dropped a primary key (do make a change) and rebuilt the PK after the change. Rebound all the packages. Trying to run RUNSTATS (with distribution and indexes all), but this time RUNSTATS is taking LOOOOOOOONG TIME to complete for all tables.
1
1967
by: batista | last post by:
Hello all, I have a third praty grid control...named C1grid. Im using it in one of my apps.. Now, I have bind this grid to a custom dataset class named "DataViewEx". The code of the class is below... Now what happens is that im updating this dataviewex class from a separate thread.....
13
4249
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any ideas on how to implement this? Thanks!
4
6391
by: Michel Esber | last post by:
Hello, DB2 V8 LUW FP 11. My applications eventually takes a very long time to process simple insert statements. Here´s the output of a event monitor: Text : INSERT INTO MyTable VALUES('000D60984D9A586AAD001128','2006-09-23-02.25.00',0,103923200,1843939328,298211000,6931647000,1834013105000,128034627001536546,6779,252559,0,33,54219,12803462700153654)
8
1634
by: so many sites so little time | last post by:
this time i am going to use the scripts from the book and just change the names to match what i am trying to do now i just got an error that i know shouldnt be an error i think. this is the error: Could add the site entry because: Column count doesn't match value count at row 1. The query was INSERT INTO home (home_id, header, body, date_entered) VALUES ('Welcome!', 'What is a Progressive Parent? A Progressive Parent ... now i created...
3
1640
by: jmark | last post by:
I have PHP 5.2.1 running on Windows XP home. The problem I have is that programs run in the command line are taking more than 10 minutes to exit. Does anyone know a solution to this?
0
1383
by: rjy24 | last post by:
Hi, I am facing problem with the following query .It is taking a lot of execution time.I need help with tuning this query. Query: SELECT A, B, C,
3
3431
by: tshad | last post by:
Using asp.net 2.0, I am finding that at times, the old javascript will still be there. I was working with it for a couple of hours and the changes seem to happen. But at the end of the day, I found that my last changes weren't taking affect. I was trying to delete my "alerts" before doing a final build but even after taking out all my alerts, when I built it and ran it - the alerts were still
4
7123
by: James Kanze | last post by:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote: Has this changed in the latest draft. According to my copy of the standard (version 1998---out of date, I know), "The operand shall be an lvalue or a qualified-id". His expression was &Test("test2"); IMHO, the compiler generated a warning because it was being laxist.
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.