Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16
  1. #11

    Quote Originally Posted by clark View Post
    Dia pa kuy problema, binary to decimal ni turboc gihapun...kung mo sobra ug 5 digits ang no na i input, lahi ang mo gawas...example type in ka ug 1100100(equivalent ni sa 100 in decmal) ang output kay -52...nganu mani

    #include <stdio.h>
    #include <math.h>

    main() {
    int dec=0,flag=0;
    int bin, bit;
    double exp=0;
    clrscr();

    printf("Enter a binary number : ");
    scanf("%d", &bin);

    while(bin) {
    bit=bin%10;
    if (bit !=0 && bit !=1) {
    flag=1;
    }
    bin=bin/10;
    dec=dec+bit*pow(2, exp);
    exp++;
    }

    printf("\nNumber in decimal : %d\n", dec);

    getch();

    }
    ang problema kay ang number nga 1100100 (decimal) di na masigo sa int nga data type (bin variable), try using long sa imong ints. pwede pud check nimo ang limits.h kay naa dira ang predefined limits sa compiler nga gigamit, INT_MAX, LONG_MAX, etc.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    	long dec = 0;
    	long flag = 0;
    	long bin = 0;
    	long bit = 0;
    	double exp = 0;
    
    	printf("Enter a binary number : ");
    	scanf("%ld", &bin);
    
    	while(bin)
    	{
    		bit = bin % 10;
    
    		if (bit != 0 && bit != 1)
    		{
    			flag = 1;
    		}
    
    		bin = bin / 10;
    		dec = dec + bit * pow(2, exp);
    
    		exp++;
    	}
    
    	printf("Number in decimal : %ld\n", dec);
    
    	return 0;
    }

  2. #12
    Quote Originally Posted by clark View Post
    I am trying to reverse a string without using "strrev"...naay sayup nya ga libug jud ko

    By the way, this is TurboC


    #include <stdio.h>
    int reverse(char r);
    main ()
    {
    int i,j;
    char a[10];
    char temp;
    clrscr ();
    printf("Enter a string: ");
    gets(a);
    reverse(a[i],temp);
    getch();
    }
    int reverse(char r){
    for (i=0;a[i]!='\0';i++);
    i--;
    for (j=0;j <= i/2 ;j++)
    {
    temp = a[j];
    a[j] = a[i-j];
    a[i-j] = temp;
    }
    printf("%s",a);
    getch();

    }
    Code:
    #include <stdio.h>
    
    /*
     * return type should be void, not an int kay
     * wala man return value ang reverse nga function.
     * parameter should be char * para ang parameter kay
     * ang memory address pointer sa actual variable.
     * diri kay char ra man, meaning iyang parameter kay
     * variable nga 1 character value ra.
     *
     * void reverse(char *);
     */
    int reverse(char r);
    
    /*
     * main should return an int value para makabalo ang OS
     * kung unsay resulta sa program, zero means way error,
     * meaning smooth-sailing ang program. the parameter
     * should be void para klaro nga way command-line arguments
     * nga gamiton ang program. di man jud ni necessary pero mas
     * mau kung mo-comply na lang daan mintras nagsugod pa lang.
     *
     * int main(void)
     */
    main ()
    {
    	/*
    	 * the declarations of "i" and "j" vars should be moved
    	 * sulod sa reverse nga function kay didto man sila gigamit.
    	 * dili dri sa main nga function.
    	 */
    	int i,j;
    
    	/*
    	 * take note, ang variable nga "a" kay 10 characters ray
    	 * masulod dala sa NULL character para terminate sa string.
    	 */
    	char a[10];
    
    	/*
    	 * same sa "i" and "j" variables, kinghanlan i-move sa
    	 * reverse nga function ang "temp" kay didto man siya gamiton.
    	 */
    	char temp;
    
    	clrscr ();
    
    	printf("Enter a string: ");
    
    	/*
    	 * dili advisable ang gets function nga gamiton kay prone
    	 * to errors or exploit ni nga function, for example, what
    	 * if ang i-input sa user kay more than 10 characters? mo-
    	 * overflow na kay 10 ra man ang maximum nga ma-hold nga
    	 * characters sa variable "a".
    	 */
    	gets(a);
    
    	/*
    	 * reverse function should only take one parameter kay mao
    	 * may naka-state sa function prototype sa ibabaw. mo-error
    	 * ni inig compile kay nag-expect man ang compiler nga
    	 * usa ra kabuok parameter unta unya duha man ang gi-supply
    	 * dri nga linya.
    	 *
    	 * reverse(a);
    	 */
    	reverse(a[i],temp);
    
    	getch();
    
    	/*
    	 * before mo-end ang program, mas maayo pahibaw-on ang OS
    	 * kung unsay resulta sa program, return 0 na lang - meaning
    	 * ok ra ang dagan sa program.
    	 *
    	 * return 0;
    	 */
    }
    
    /*
     * return type sa reverse function should be void kay wa may
     * return value, parameter should be a char pointer para diretso
     * manipulate sa data. unya instead of "r", "a" unta dapat
     * gamiton kay mao man gigamit nga variable name sa ubos (sa loop).
     *
     * void reverse(char *a){
     */
    int reverse(char r){
    	/*
    	 * variables "i", "j", and "temp" should be declared first
    	 * before sila pwede gamiton.
    	 */
    
    	for (i=0;a[i]!='\0';i++);
    	i--;
    	for (j=0;j <= i/2 ;j++)
    	{
    	temp = a[j];
    	a[j] = a[i-j];
    	a[i-j] = temp;
    	}
    	printf("%s",a);
    
    	/*
    	 * no need na ang getch() diri dapita kay once mahuman ni nga
    	 * function, naa may mosunod nga getch() function.
    	 */
    	getch();
    
    	/*
    	 * return 0 ta ni dapat kay gi-declare man nga int type
    	 * ang return value ani nga function, kay wa may gi-return.
    	 * i-void na lang ni nga function.
    	 */
    }
    mao ni imong code bro with the appropriate changes para modagan siya...

    Code:
    #include <stdio.h>
    
    void reverse(char *);
    
    int main(void)
    {
    	char a[10];
    	clrscr();
    	printf("Enter a string: ");
    	gets(a);
    	reverse(a);
    	getch();
    	return 0;
    }
    
    void reverse(char *a)
    {
    	int i, j;
    	char temp;
    	for(i = 0; a[i] != '\0'; i++);
    	i--;
    	for(j = 0; j <= i / 2; j++)
    	{
    		temp = a[j];
    		a[j] = a[i - j];
    		a[i - j] = temp;
    	}
    	printf("%s", a);
    }

  3. #13
    @darkhero - Sir, daku jud kaau ko ug pasalamat nmo Nka sabot najud ko ug maau....ako2x ra man gud ni na program...Dli kaau ko kasabot sa tinudloan sa among maestra ani...Wew!!! Salamat jud kaau sir!!! God bless you sir!!!

  4. #14
    no probs

  5. #15
    Hastilan dugaya na nko last aning turbo C uy, nalimot na man ko ani. Hehehe..

  6. #16
    #include <stdio.h>
    #include <math.h>

    main() {
    int dec=0,flag=0;
    int bin, bit;

    /*
    I suggest int bin should be char* bin or char bin[100] para daghan ang mga characters nga imung ma-input. Dili lang limited sa 5 chars.
    */

    double exp=0;
    clrscr();

    printf("Enter a binary number : ");
    scanf("%d", &bin);

    while(bin) {
    bit=bin%10;
    if (bit !=0 && bit !=1) {
    flag=1;
    }
    bin=bin/10;
    dec=dec+bit*pow(2, exp);
    exp++;
    }

    printf("\nNumber in decimal : %d\n", dec);

    getch();

    }


    Here is the modified code:


    #include <stdio.h>
    #include <math.h>

    main() {
    int dec=0,flag=0;
    int bit;
    char* bin = calloc(sizeof(char), 50); // CHANGED. check lang basin bali ang parameters
    int i = 0; // ADDED. For loop
    double exp=0;
    clrscr();

    printf("Enter a binary number : ");
    gets(bin); // CHANGED

    for(i = 0; i < strlen(bin); ++i) { // CHANGED
    bit=(bin[i] - 0x30)%10; // CHANGED. Since bin is already a string. -0x30 is to convert
    // a character to int. So '1' becomes 1.
    if (bit !=0 && bit !=1) {
    flag=1;
    }
    // NOT NEEDED. bin=bin/10;
    dec=dec+bit*pow(2, exp);
    exp++;
    }

    printf("\nNumber in decimal : %d\n", dec);

    getch();

    }

  7.    Advertisement

Page 2 of 2 FirstFirst 12

Similar Threads

 
  1. unsay problema sa akong motor? pls help
    By paploka in forum Automotive
    Replies: 17
    Last Post: 02-10-2013, 06:25 PM
  2. UNSAY GUBA SA AKONG HARD DISK????
    By MisterSuave in forum Computer Hardware
    Replies: 20
    Last Post: 12-11-2011, 07:19 AM
  3. masters sa simple win xp. need help asap please
    By RebC in forum Software & Games (Old)
    Replies: 22
    Last Post: 03-03-2008, 01:10 PM
  4. Question sa akong PC.... i need assistance
    By blackmiller in forum Computer Hardware
    Replies: 6
    Last Post: 01-29-2006, 05:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top