Thread: Simple program with my little initiative (code+explanation required)

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

    Simple program with my little initiative (code+explanation required)

    Hi!


    I'm looking for someone who woudl be keen to guide me and explain to me how to write this program in ANSI C.
    I'm not looking for some crib, mainly I would like to understand the code. So maybe some code with additional comments ?


    In order not to look like look like a shirker, I'm keen to give you some personal suggestion about algorhytm.
    Unfortunately, currently I'm not capable of spending any money on my personal project, so disinterested people are required.


    Ok, here's the task.


    Workers at the office have to visit their clients constanstly. The office policy is very enviromently friendly,
    that is why the use only electric engined cars. Unfortunately cars they're using are not sufficient, nor have long
    range without recharging. Additionaly, the office require from it's workers to have obligatory pause at work. Due to
    this unique regulations, workers have to stop their cars and lodge in roadside motels EVERY TIME their cars run out of juice.
    Write the program, which will help the worker to plan his journey, so that he won't stop at the motels unnecessarily.
    Most efficient solutions required (less stops means less motels, ergo less expenses).


    Program has to detect situation in which motels are too far away from each other, in which case it should
    display some massage, for instance: ERROR !


    INPUT


    Program will be given certain amount of kilometres to cover (N) on certain route,
    range of electric car which could be exploit per ONE DAY (K) and (M) number of motels which are
    available on the journey.


    We assume that in m- lines there will be given distances between certain motels from the start of the journey.
    We assume that distances are represented by integral numbers.

    Distances between each motels will be ordered by size (number of kilometres), from the shortest distance to the longest.


    1<=n<=1000000
    1<=k<=5000
    0<=m<=1000000


    OUTPUT


    Program is meant to display on OUTPUT minimal number of stops, which is needed to cover the distance which is received by program
    at the beggining. If journey with specified paramateres will not live up to given requirements, program should display message
    for instance WRONG SPECIFICATION !. We assume that at the beggining the batteries in the car are fully charged up !


    Example


    IN


    3000
    800
    6
    100
    200
    850
    1600
    2300
    2900


    OT


    4


    Here's my IDEA ! ALGORYTHM !


    Let's put distances between motels to the array. Then use some if statements to check if there's is enough distance to cover by the vehicle to reach destination of next motel.


    I've have some code, but I've only manage to declare variables and did something strange with if().


    Could you please, help me out with this ?


    I would like to understand the code, so I'm not expecting only pure code without comments.
    If you're keen to help me, contact me via e-mail: panzyciaismierci1(at)interia.pl
    It's quite important not to publicate any solution in this thread, cause it will simply ruin my anonymous and my academic teacher would persumably kill me

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you trying to implement this for real world use, or are you just messing around trying to get some practice? If you drive as much as I do you should know that the quickest or most economical way between two points is not necessarily the shortest route. If you want to cross central London at rush hour from West to East the shortest route is through the center, however that will require a good three hours of your time. If you use the beltway you might reduce your travel time by up to 50%. Just something to think about.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Well, if I tried to implement this for real world use, I wouldn't be posting this thread, cause simply, as professional programmer who has been given some task I would know how to write this code. Unfortunately, I'm in a hurry, I'm doing this for my study. I was trying many different ways to even write this code, because I didn't even get to this point when I can compile it ! My teacher is quite badass, and after 2 months of learning ANSI C, he requires to write programs at this level of "difficulty"...
    It may sound ridiculous ! But it's true, I'm not writing this just to convince you that I'm not lazy slacker, but to give you an idea that simply I'm not capable of writing this program on my own !

    Cheers grosjean

    Also I would like to point out, that we're using ONLY ONE WAY. Which means, we're moving only in one direction in straight line, it's like imaging straight line with dots on it (which symbolize clients) !
    Last edited by jensonbutton; 04-25-2012 at 09:51 AM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, well unfortunately the way it works here is : no code - no help. I suggest you read the homework policy for the forum.
    If however, you are willing to do the work, and post here questions along the way, then that is a completely different story.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Would it help if I post some basic lines in code, being about less than 10% of total code's lenght. Trust me, I would love to do this HM on my own, unfortunately don't have enough skills and can't mull it over since I'm learning ANSI C for 2 months.
    If YES, I will post my "basic" code as soon as possible.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    People here will not write functionality for you. They will tell you how to approach issues, how to model your data and how to debug your program, but they won't actually do it for you. You have to do the work, because it is your homework and we won't help non-programmers take our jobs.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Oh... c'mon, do you believe that I will ever be able to threat your job ?

    I won't poste 100% of code, cause SIMPLY I'M TO FRESH-FACED to do it, I can show you my initiative by posting something that I'm capable of, which are elementary, basic things, I'm keen to learn something, but don't have enough knowledge to use non-familiar options which probably will solve my problem...


  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you were not too fresh-faced to take the class, you are not too fresh faced to do the homework.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Yeah, you're absolutely right. Unfortunatenly, my teacher isn't bright enough and he likes to punish his students. I will post code anyway. Maybe someone will understand me.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Here's something I've managed to write.

    Code:
    #include <stdio.h>
    
    int main(){
    
    int N, K, M, i, distance_motel_start=0, tab_of_total_distance[];
    
    printf("Please type, distance (in km) you want to cover on the route\n");
    scanf("%d", N);
    printf("Please type, range (in km) of your car per 1 day\n");
    scanf("%d", K);
    printf("Please type number of motels which are available on the journey\n);
    scanf("%d", M);
    
    int i=1;
    while (i<M) {
    ++i;
    printf("Please type distance between the motel and starting point);
    scanf("%d", &distance_motel_start);
    tab_of_total_distance[]++;
    }
    
    if(tab_of_total_distance[]>N){
    printf("WRONG SPECIFICATION");
    }
    
    return 0;
    }
    I'm wondering if it's any good...
    Last edited by jensonbutton; 04-25-2012 at 11:56 AM.

  11. #11
    Registered User Alan.Brown's Avatar
    Join Date
    Jul 2010
    Location
    NSW, Australia
    Posts
    9
    Have you tried to compile and run your code. That should be an easy task - even for you.

    If you have, post the output here. Maybe it works!! Beginners luck!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-26-2011, 11:35 AM
  2. Hello!a simple optimisation required!
    By chottachatri in forum C Programming
    Replies: 10
    Last Post: 10-19-2008, 07:06 AM
  3. Simple Explanation Needed
    By slowcoder in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 02:00 PM
  4. Explanation required (Why?)
    By pritin in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 08:47 AM
  5. Hints required to complete simple programs
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:20 PM

Tags for this Thread