Thread: Basic problem using for loops w/ functions. help :(

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Basic problem using for loops w/ functions. help :(

    Would someone help me with this word problem?

    Write a function that will accept three integers as parameters, call these three integers as start , end and step respectively.


    -assume that start is always less than end and step is a positive integer greater than zero.


    -thereafter, the function should print the numbers such that the first item is start, the second item is equivalent to the first item plus the value of step, the third item is equivalent to second item plus the value of step and so on. the last value to be printed should not be greater than end.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    can you help me with this ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Probably. You should make an attempt first though, and that is what Salem is getting at.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Code:
    #include<stdio.h> void notepad(int,int,int); void main() { int a,b,c; notepad(a,b,c); } void notepad(int a,int b, int c) { int start,end,step; printf("Enter the 1st Number :"); scanf("%d",&start); printf("Enter the 2st Number :"); scanf("%d",&end); printf("Enter the 3st Number :"); scanf("%d",&step); for(start=start;start<end;++start) printf("%d\n",start); }
    is this correct? im still newbie for C

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first step would be for you to show that you can either
    - call a function with parameters, but can't do the loop
    - can do the loop in main, but somehow cannot call a function
    - can do a loop, but only if step is 1

    Now, where exactly are you stuck?

    Edit:
    Try start += step
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    where i would put "start+=step" ? sorry.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Where in the code you have at the moment do you see start being modified?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why do you pass a, b, and c to your function? They aren't used or modified.

    Don't use void main: SourceForge.net: Void main - cpwiki

    > where i would put "start+=step" ?
    You're making me think you didn't write the above code. Just think about it. In the loop, instead of incrementing by 1 every time, increment by step.

    Code:
    for(start=start;start<end;++start)
    You don't need "start=start;".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C functions using loops
    By Noobpoint in forum C Programming
    Replies: 19
    Last Post: 02-13-2012, 10:31 PM
  2. Problem with nested loops in functions
    By countchocula in forum C Programming
    Replies: 5
    Last Post: 04-21-2008, 03:10 PM
  3. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  4. Functions and loops assignment problem
    By JFonseka in forum C Programming
    Replies: 2
    Last Post: 08-07-2007, 03:21 AM
  5. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM