TransWikia.com

Using function and pointer to reduce fraction to lowest term

Stack Overflow Asked on December 6, 2020

Please help me with my code!

#include <stdio.h>

void reduce(int numerator, int denominator, int *reduce_numerator, int *reduce_denominator);
int gcd;
int *reduce_numerator, *reduce_denominator;

int main(void){
 
  int numerator,denominator;
  
  printf("Enter a fraction:");
  scanf("%d/%d", &numerator, &denominator);
  
  int new_numerator, new_denominator;
  
  reduce_numerator = &numerator;
  reduce_denominator = &denominator;
 
  reduce(numerator, denominator, reduce_numerator, reduce_denominator);
  
  new_numerator = numerator / gcd;
  new_denominator = denominator / gcd;
  printf("%d%d", new_numerator, new_denominator);
  
  reduce_numerator = &new_numerator;
  reduce_denominator = &new_denominator;
  
  printf("In lowest terms:%d/%d", *reduce_numerator, *reduce_denominator);
  
  int result = numerator / denominator;
  printf("The value is:%d", result);
  return 0;
}

void reduce(int numerator, int denominator, int *reduce_numerator, int *reduce_denominator){
    
    int gcd, remainder;
    
    while(*reduce_numerator!=0){
        remainder = *reduce_denominator % *reduce_numerator;
        *reduce_denominator = *reduce_numerator;
        *reduce_numerator = remainder;
    }
    gcd = *reduce_denominator;
  }

I don’t know the reason that I can’t recall the function in the "main function", so if anyone know, please help me and let me understand, thanks very much!

p.s. The "reduce" function as well as its form are requested to have!

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP