Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,690 members, 7,809,606 topics. Date: Friday, 26 April 2024 at 11:56 AM

C Programming Issues - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / C Programming Issues (2469 Views)

Introduction To C++ Programming (Video Tutorial For Beginners) / C Programming Challenge To Be Solved Under 10 Minutes (2) (3) (4)

(1) (Reply) (Go Down)

C Programming Issues by 8figure(m): 5:03pm On Dec 29, 2011
Below is the source,compilation and output of a little C program, I would like the c programmers in the house to give me details on why the output is not as expected.

Re: C Programming Issues by Bossman(m): 5:18pm On Dec 29, 2011
Not a C programmer but, I suspect the amount is not being read correctly. Why don't you print the amount as soon as you read it. I bet it will be 0. Then you can focus on why it's not being read correctly.
Re: C Programming Issues by Nobody: 5:59pm On Dec 29, 2011
Re: C Programming Issues by Kobojunkie: 8:50pm On Dec 29, 2011
The output is correct @Poster. You probably meant to use different types i.e double [/b]or [b]decimal, but the answer you are getting is correct since you are using int type for you amount and result_1 and result_2.

Please brush up on your C data types and pay attention to the nuances.

http://en.wikipedia.org/wiki/C_data_types
Re: C Programming Issues by 8figure(m): 9:04pm On Dec 29, 2011
@omo_to_dun,  you got what I needed man, I never thought of casting int  to float  type and specifier as well,  since my division was gonna result in floating point .
muchos gracias , let me recompile and extend its capabilities.@all am just learning c with everything around me, so I may  disturb your peace with things like this . I know You might have guest the origin of that one.

Re: C Programming Issues by Fayimora(m): 9:43pm On Dec 29, 2011
Don't know if you picked this up along the line but it is advisable to always initialise a variable before using it.
Re: C Programming Issues by IbroSaunks(m): 11:06pm On Dec 29, 2011
Just seeing it, though it has already been pointed out, imma just say it again! You did't initialize amount before using it to calculate result_1 and result_2;
Re: C Programming Issues by delocknomean: 5:02am On Dec 30, 2011
You don't calculate your value in declaration part and you use same arithmetic calculation in body part, and check again your program result.
Re: C Programming Issues by 8figure(m): 10:31am On Jan 05, 2012
Please folks, I can't seem to find the difference between these two codes, but they are not
printing the same thing, could anybody help fix the bug.




#include <windows.h>
#include <stdio.h>

void SlowPrint(char* string,int sleeptime)
{
int i =0;
while (string[i] != '\0')
  {
      printf("%c",string[1]);
      Sleep(sleeptime);
      i++;
  }
}
int main()
{
    SlowPrint("De hero is here",50);
    Sleep(2000);
    return 0;
    }


##########################################################




#include <windows.h>
#include <stdio.h>

void SlowPrint(char* string, int sleeptime)
{
     int i = 0;
     while(string[i] != '\0')   
     {       
             printf("%c", string[i]);
             Sleep(sleeptime);
             i++;
     }
}

int main()
{
    SlowPrint("Hi! You're looking at an example of the slowprint function!", 50);
    Sleep(2000);
   
    return 0;
}       
Re: C Programming Issues by Analytical(m): 11:53am On Jan 05, 2012
8figure, even if the bug is fixed, your programs will still not print the same thing!  Reason- the values passed into 'string' are not the same!

I guess you meant the first program repeatedly prints the character 'k' (or the 1st character of any string passed) while the 2nd prints the whole string.  Reason- you used '1' instead of 'i' in the statement printf("%c",string[1]); in your 1st program!!!!
Re: C Programming Issues by 8figure(m): 5:08pm On Jan 05, 2012
Ok I found that, Now my problem again is ; since the code is for slow printing, and it is supposed to do the same thing as
Sleep() that resides in windows.h file, what is then the need for including the Sleep function, where passing value in mm seconds to
slowprint() would produce the same effect.

And also I noticed that the speed difference between the two functions.

More light please.
Re: C Programming Issues by lordZOUGA(m): 6:03pm On Jan 05, 2012
maybe because the second string is longer than the 1st one,
Re: C Programming Issues by Analytical(m): 7:59am On Jan 06, 2012
Right!  The 1st string in your modified post is 15 characters long while the 2nd string is 59 characters (including spaces).  So all things being equal, the 1st routine will run in approximately 25% of the time taken by the 2nd to run.

Note: your SlowPrint routine still made use of the Sleep function.  They are not supposed to do the same thing.  Sleep introduces delay/pause (and can be use anywhere you want to cause a delay) while your SlowPrint prints a string passed into it one character at a time with a delay.
Re: C Programming Issues by 8figure(m): 8:54am On Jan 06, 2012
^^NOTA BENE; am not talking of speed based on the two codes, sorry I had to modify the code when i found the problem.
am talking of speed based on the two functions, Sleep() and Slowprint(),  example is , when I pass value of 500 to both, using only Sleep() in code A and using only slowprint() in code B the delay time is never the same result, does it have anything to do with codes in windows.h file .

light please.
Re: C Programming Issues by lordZOUGA(m): 10:17am On Jan 06, 2012
Slowprint() is a function defined using sleep(), If the sleep() executes slowly then slowprint() will execute slower,
Re: C Programming Issues by Analytical(m): 10:40am On Jan 06, 2012
lordZOUGA is correct again. Your SlowPrint() function uses sleep() in a loop that repeats i number of times where i is the length of the string passed (ie 15 and 59). So if sleep(500) executes in 500milliseconds, expect SlowPrint(string,500) to execute in 500 x len(string) milliseconds at the minimum!
Re: C Programming Issues by 8figure(m): 2:33pm On Mar 27, 2012
Hello Guys;
its been a long time ,I've been studying studying and stu... other stuffs(scripting langs), and now am back to my c++ and just to extend this prog, it kept printing the first word, but I want it to print the whole input from cin.please any explanation on how i could do this.B plays fine, but A messed up cos i wanted the user to control what should be printed.




Also I have been trying to implement quick sort algo, but just learning the algo without practical implementation, its not just only quick sort algo ,but many other also. Any example instance where i can implement such algos would be appreciated.

muchos gratias


8figure

Re: C Programming Issues by 8figure(m): 1:51pm On Mar 28, 2012
Choi No reply yet? nawa o, so you guys don't want new programmers to emerge.

Well if I wait for the next 8hr, I will do it without.

(1) (Reply)

How To Program Arduino Uno / Is Data Science Worth It In Nigeria / I Find It Hard Learning Javascript...i Need Help

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.