473,399 Members | 4,177 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,399 software developers and data experts.

How do I call something in to a for loop?

283 100+
Hi im new to C++ and im trying to call a function in to a for loop.


Expand|Select|Wrap|Line Numbers
  1.  
  2. void DrawLine(int x1, int y1, int x2, int y2, unsigned int color)
  3. {
  4.  
  5. int dx, dy;                         // dy / dx is the slope
  6.     int x, y;                           // loop and point variables
  7.  
  8.     // calculate changes in y and x between the points
  9.     dy = y2 - y1;
  10.     dx = x2 - x1;
  11.  
  12.     if (Abs(dy) > Abs(dx)) {
  13.         // since there is a greater change in y than x we must
  14.         // loop in y, calculate x and draw
  15.         for (y=y1; y != y2; y += Sign(dy)) {
  16.             x = x1 + (y - y1) * dx / dy;
  17.             PGraphics->AddPoint(x, y, color);
  18.         }
  19.     }
  20.     else {
  21.         // since there is a greater (or equal) change in x than y we must
  22.         // loop in x, calculate y and draw
  23.         for (x=x1; x != x2; x += Sign(dx)) {
  24.             y = y1 + (x - x1) * dy / dx;
  25.             PGraphics->AddPoint(x, y, color);
  26.         }
  27.     }
  28.  
  29.     // draw the last pixel
  30.     PGraphics->AddPoint(x2, y2, color);
  31.  
  32.     // draw the points
  33.     PGraphics->Draw();
  34. }
  35.  
  36.  
  37. void DrawStuff() {
  38.     COLORREF green = RGB(0, 255, 0);     // green color to draw with
  39.     COLORREF purple = RGB(255, 0, 255);  // purple color to draw with
  40.  
  41.     char str[32];                       // string to store user input
  42.     int h, k;                           // parabola vertex
  43.     double a;                           // parabola constant - might be a decimal
  44.     int x, y;                           // loop and point variables
  45.     int xPrev, yPrev;                   // previous point for drawng line segments
  46.     int ymin, ymax;                     // limits for y loop
  47.     RECT rect;                          // rectangle for the output window
  48.  
  49.     // get the user input from the edit boxes and 
  50.     // convert string input to integer
  51.     GetDlgItemText(HDialog, IDC_EDIT_VERTEXX, str, 32);
  52.     h = atoi(str);
  53.     GetDlgItemText(HDialog, IDC_EDIT_VERTEXY, str, 32);
  54.     k = atoi(str);
  55.     GetDlgItemText(HDialog, IDC_EDIT_CONSTA, str, 32);
  56.     a = atof(str);                              // use atof to allow user to enter a decimal
  57.  
  58.     // get the rect for this window
  59.     GetClientRect(HOutput, &rect);
  60.  
  61.     // use the rectangle info to set up y loop limits
  62.     ymin = -(rect.bottom - rect.top) / 2;
  63.     ymax =  (rect.bottom - rect.top) / 2;
  64.  
  65.     // clear the scene and add an axis
  66.     PGraphics->ClearScene(RGB(0, 0, 0));
  67.     PGraphics->AddAxis(RGB(150, 150, 150), 10);
  68.  
  69.     yPrev = ymin;
  70.     xPrev = (int)( a * (yPrev-k) * (yPrev-k) ) + h;
  71.  
  72.     // loop in y, calculate x and draw
  73.     for (y = ymin; y <= ymax; y++) {
  74.         x = (int)( a * (y-k) * (y-k) ) + h;
  75.         PGraphics->AddPoint(x, y, green); 
  76.     }
  77.  
  78.     // draw the points
  79.     PGraphics->Draw();
  80.  
What im trying to do is take the DrawLine function (that I put in bold) and have it referenced right before the for loop in the DrawStuff (that i put in bold) This way the DrawStuff will know to use DrawLine to draw more points in a line.

Thanks in advance!
Mar 14 '10 #1
4 2304
weaknessforcats
9,208 Expert Mod 8TB
The only wat that DrawStuff can know to use DrawLine is for you to a) call Drawline inside DrawStuff or b) have a function pointer argument in DrawStuff that contains the adress of the correct DrawLine function.


Expand|Select|Wrap|Line Numbers
  1. void DrawStuff()
  2. {
  3.      DrawLine(etc...);
  4.      for (etc...
  5.      {
  6.          ///or  DrawLine(etc...);
  7.      }
  8. }
Mar 14 '10 #2
whodgson
542 512MB
Do you mean to spell ymin like ymi.n
Mar 15 '10 #3
slenish
283 100+
Hi weaknessforcats,
Appreciate you helping me out with this I was wondering if you could give me a little more detail if your example or possibly give me a couple websites with some examples I could look at.

Thanks for the help!


Whodgson,
its supposed to be ymin. :) It refers to the min value of y
Mar 15 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
Hi weaknessforcats,
Appreciate you helping me out with this I was wondering if you could give me a little more detail if your example or possibly give me a couple websites with some examples I could look at.
It may be you are no clear on how a function call works.

Your program excutes statements in sequence. A B C D. If you have steps that are used in several places, you code A XXX B XXX C XXX D XXX. Those XXX instrcutions are repeated as many times a needed.

A function call is a way of coding XXX only once. These instructions are put together using a name an argument list and a pair of braces:

Expand|Select|Wrap|Line Numbers
  1. void MyFunction()
  2. {
  3.       XXX
  4.  
  5. }
The code now looks like:

A MyFunction() B MyFunction() C MyFunction() D MyFunction().

The last thing is when you use a variable as an argument to a function, the compiler makes a copy of it. The copy is the argument variable of the function being called. This is done to protect the original variable.

You can find all sorts of information about this from just about any book on C.
Mar 16 '10 #5

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

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
4
by: Kevin Schultz | last post by:
Hello all. I am interfacing my computer to the outside world for an experiment and we would like to know how much time the calling function takes. The specs say this should happen on the order of a...
2
by: Jack Addington | last post by:
I am working on app that currently all resides on the same machine but plan to pull the database off and try to move all the datafunctionality to a remote machine. I have a data function that is...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
1
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am playing around with a variation of the ".NET...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
19
by: Riko Wichmann | last post by:
hi everyone, I'm googeling since some time, but can't find an answer - maybe because the answer is 'No!'. Can I call a function in python inline, so that the python byte compiler does...
1
by: Eric | last post by:
When I run my script it gives error on the following line: strEmail = Right(strEmail, (Len(strEmail) - 1)) I enclose my code and the sample text file too Thanks,...
3
by: crater | last post by:
Today I ran into a problem that had me completely baffled. A function that I have just written, processes data in any one of 4 different data grids. Within a for() loop I need to call another...
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
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,...
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
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
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...
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.