Jump to content

C function code help.

duckwithanokhat
Go to solution Solved by spenser_l,

You would be using function 1 to extract the most significant digit, and the second function to find the remainder, and repeat.

 

Since your range is 1 - 32767 (215) it makes sense that the example output shows 0 4 5 6 2

 

So input is 4562

 

4562 / 10000 = 0 (integer division)

4562 % 10000 = 4562

 

4562 / 1000 = 4

4562 % 1000 = 562

 

562 / 100 = 5

562 % 100 = 62

 

62 / 10 = 6

62 % 10 = 2

 

2 / 1 = 2

 

printing the output we get using the first function (which gives us the most significant digit): 0 4 5 6 2

 

I'll leave it to you as to how to properly store and loop through using the 2 functions you already made.

So I got a lab in my computer science class, here is the spec: https://gyazo.com/e51466b4478f672b0c2733ac78a7344b

 

Basically says write a function that does integer division and one that has modulus.

 

The second part of the lab is to use the two functions to write a program that can input a number between 1 and 32767 and display them as a series of digits each with 2 spaces between them.

 

My question is how do these two concepts relate to each other and how would you perform this?

 

EDIT: Please don't give me exact code to copy and paste, just ideas please :D. I also have written the code for the two functions if anyone wants to see them.

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, duckwithanokhat said:

The second part of the lab is to use the two functions to write a program that can input a number between 1 and 32767 and display them as a series of digits each with 2 spaces between them.

if mean to print them from 1 to 32767 sequentially-

you can use the largest number to check whether you have reached the end by checking the remainder has reached 0 using while loop, in which you use the increasing value of variable which started with minimum value.

 

ps: wrote it as a hint

(I am not able to access the link you provided due to access restriction)

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, beingGamer said:

if mean to print them from 1 to 32767 sequentially-

you can use the largest number to check whether you have reached the end by checking the remainder has reached 0 using while loop, in which you use the increasing value of variable which started with minimum value.

 

ps: wrote it as a hint

(I am not able to access the link you provided due to access restriction)

Ahh no, just input a random number between 1 and 32767, here is an embed picture if that helps:e51466b4478f672b0c2733ac78a7344b.png

Link to comment
Share on other sites

Link to post
Share on other sites

You would be using function 1 to extract the most significant digit, and the second function to find the remainder, and repeat.

 

Since your range is 1 - 32767 (215) it makes sense that the example output shows 0 4 5 6 2

 

So input is 4562

 

4562 / 10000 = 0 (integer division)

4562 % 10000 = 4562

 

4562 / 1000 = 4

4562 % 1000 = 562

 

562 / 100 = 5

562 % 100 = 62

 

62 / 10 = 6

62 % 10 = 2

 

2 / 1 = 2

 

printing the output we get using the first function (which gives us the most significant digit): 0 4 5 6 2

 

I'll leave it to you as to how to properly store and loop through using the 2 functions you already made.

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

ok, I get it.

Basically they are asking you to write a program that will separate out single digits from the number you will input and separate them each with double spaces.

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, spenser_l said:

You would be using function 1 to extract the most significant digit, and the second function to find the remainder, and repeat.

 

Since your range is 1 - 32767 (215) it makes sense that the example output shows 0 4 5 6 2

 

So input is 4562

 

4562 / 10000 = 0 (integer division)

4562 % 10000 = 4562

 

4562 / 1000 = 4

4562 % 1000 = 562

 

562 / 100 = 5

562 % 100 = 62

 

62 / 10 = 6

62 % 10 = 2

 

2 / 1 = 2

 

printing the output we get using the first function (which gives us the most significant digit):

So would I make 5 variables to store each digit in?

 

Edit: How would I save the result from mod?

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, duckwithanokhat said:

So would I make 5 variables to store each digit in?

 

Edit: How would I save the result from mod?

Sure, or you could use an integer array.

 

This is going to be in a separate function entirely, so you would need a variable to save that output.

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, duckwithanokhat said:

So would I make 5 variables to store each digit in?

 

Edit: How would I save the result from mod?

use a string and keep pre-pending the remainders you will get

or you can use an integer array and add spaces while printing them using a loop

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

Hallelujah! I did it!!!! It took me so long, thank you for telling me what to do guys :) .

 

Edit: Although I am going to ask my teacher about the efficiency of my code because I ended up making 12 functions and 11 variables...

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, duckwithanokhat said:

Hallelujah! I did it!!!! It took me so long, thank you for telling me what to do guys :) .

 

Edit: Although I am going to ask my teacher about the efficiency of my code because I ended up making 12 functions and 11 variables...

that's too much.

you can do this in 2 variables & 2 functions only(+ main function)

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, beingGamer said:

that's too much.

you can do this in 2 variables & 2 functions only(+ main function)

Since I finish my project, I feel less guilty about asking for specific code, so can you show me how that code would look like?

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, duckwithanokhat said:

Hallelujah! I did it!!!! It took me so long, thank you for telling me what to do guys :) .

 

Edit: Although I am going to ask my teacher about the efficiency of my code because I ended up making 12 functions and 11 variables...

Definitely go over the problem with your teacher, it's important that your function can be applied to different inputs. That seems like too much to me.

 

For reference, here is my solution in C (there are probably better ones since this does no input validation (1-32767), but for the simple case):

 

#include <stdio.h>

int int_div(int x, int y ) {
    return x / y;
}

int rem(int x, int y) {
    return x % y;
}

int* msd(int x) {
    int i = 0;
    int place = 10000;
    int remain = 0;

    static int output[5];

    while (place >= 1) {
        output[i++] = int_div(x, place);
        remain = rem(x, place);
        place = place / 10;

        if (remain != 0) {
            x = remain;
        }
    }

    return output;
}

int main()
{
    int *p;
    int i;

    p = msd(4562);

    for ( i = 0; i < 5; i++ ) {
        printf("%d ", *(p + i));
    }

    printf("\n");

    return 0;
}

 

Please don't just copy this, it won't help your learning. Definitely talk to your teacher about how you can improve the code that you wrote.

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, duckwithanokhat said:

Since I finish my project, I feel less guilty about asking for specific code, so can you show me how that code would look like?

sure, but you will have to wait a bit.

Will write and share once I reach home

7-8 hours to go :/

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, spenser_l said:

Definitely go over the problem with your teacher, it's important that your function can be applied to different inputs. That seems like too much to me. For reference, here is my solution (there are probably better ones):

 


#include <stdio.h>

int int_div(int x, int y ) {
    return x / y;
}

int rem(int x, int y) {
    return x % y;
}

int* msd(int x) {
    int i = 0;
    int place = 10000;
    int remain = 0;

    static int output[5];

    while (place >= 1) {
        output[i++] = int_div(x, place);
        remain = rem(x, place);
        place = place / 10;

        if (remain != 0) {
            x = remain;
        }
    }

    return output;
}

int main()
{
    int *p;
    int i;

    p = msd(4562);

    for ( i = 0; i < 5; i++ ) {
        printf("%d ", *(p + i));
    }

    printf("\n");

    return 0;
}

 

Please don't just copy this, it won't help your learning. Definitely talk to your teacher about how you can improve the code that you wrote.

Uh huh... even if I did want to copy it I would get caught right away, because we haven't learned "for" "if" etc. So I can't use it plus can't understand it, sorry.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, duckwithanokhat said:

Uh huh... even if I did want to copy it I would get caught right away, because we haven't learned "for" "if" etc. So I can't use it plus can't understand it, sorry.

Sorry I wasn't exactly sure what concepts you've covered. If you haven't been taught loops yet then its to be expected that you'll have to write more code. Best bet is to go over your solution with your teacher.

 

Once you've gone over loops/conditionals you can come back to my code and tell me what's wrong with it :P (there's already a case that it fails)

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, spenser_l said:

Sorry I wasn't exactly sure what concepts you've covered. If you haven't been taught loops yet then its to be expected that you'll have to write more code. Best bet is to go over your solution with your teacher.

 

Once you've gone over loops/conditionals you can come back to my code and tell me what's wrong with it :P (there's already a case that it fails)

Lol, thanks for the effort anyways!

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, duckwithanokhat said:

Since I finish my project, I feel less guilty about asking for specific code, so can you show me how that code would look like?

Sorry, I didn't have C on my PC. So here's a C# version, just so that you can understand the logic.

I'll try to share C/C++ version when I find it.

 

class Program
    {
        static void Main(string[] args)
        {
            int x,counter=0;

            x = int.Parse(Console.ReadLine());

            int[] array = new int[x.ToString().Length];
            while (x > 0)
            {
                array[counter++]= mod(x, 10);
                x = divide(x, 10);
            }

            while (counter>0)
            {
                Console.Write(array[--counter]+"  ");
            }
            Console.ReadKey();
        }

        public static int divide(int x, int y)
        {
            return x / y;
        }

        public static int mod(int x, int y)
        {
            return x % y;
        }        
    }

 

I intentionally used array instead of list here, so it will be understandable to you.

If list was used, variable named 'counter'there wouldn't be required.(so, 2 functions & 1 variable as I mentioned earlier)

 

Another thing is, in above code if you want to skip non-numeric input, you can put 

while (int.TryParse(Console.ReadLine(), out x))
            {
                Console.Clear();
                Console.Write("Please enter a number:");                
            }                

instead of 

x = int.Parse(Console.ReadLine());

to keep asking for input till user enters a numeric value.

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, beingGamer said:

Sorry, I didn't have C on my PC. So here's a C# version, just so that you can understand the logic.

I'll try to share C/C++ version when I find it.

 


class Program
    {
        static void Main(string[] args)
        {
            int x,counter=0;

            x = int.Parse(Console.ReadLine());

            int[] array = new int[x.ToString().Length];
            while (x > 0)
            {
                array[counter++]= mod(x, 10);
                x = divide(x, 10);
            }

            while (counter>0)
            {
                Console.Write(array[--counter]+"  ");
            }
            Console.ReadKey();
        }

        public static int divide(int x, int y)
        {
            return x / y;
        }

        public static int mod(int x, int y)
        {
            return x % y;
        }        
    }

 

I intentionally used array instead of list here, so it will be understandable to you.

If list was used, variable named 'counter'there wouldn't be required.(so, 2 functions & 1 variable as I mentioned earlier)

 

Another thing is, in above code if you want to skip non-numeric input, you can put 


while (int.TryParse(Console.ReadLine(), out x))
            {
                Console.Clear();
                Console.Write("Please enter a number:");                
            }                

instead of 


x = int.Parse(Console.ReadLine());

to keep asking for input till user enters a numeric value.

Sorry, I can't really understand this, so far I know how to make a function, math operations, printf, scanf, puts, printf specifiers, declaring variables and constants (no static), post/pre-increment and accumulators (although I don't think I'm supposed to use them in this program because I only know the theory). That's about it. Oh and btw I'm not learning C++ yet, just ole C.

 

I also asked my teacher and he said, yes, I am using too many functions (I think I had 12) and that I should use one or two to do all my calculations. Any idea how to do all the calculations with 1 modulus and 1 division function?

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, duckwithanokhat said:

Sorry, I can't really understand this, so far I know how to make a function, math operations, printf, scanf, puts, printf specifiers, declaring variables and constants (no static), post/pre-increment and accumulators (although I don't think I'm supposed to use them in this program because I only know the theory). That's about it. Oh and btw I'm not learning C++ yet, just ole C.

 

I also asked my teacher and he said, yes, I am using too many functions (I think I had 12) and that I should use one or two to do all my calculations. Any idea how to do all the calculations with 1 modulus and 1 division function?

My idea is that since I have to divide and mod a number by 10000, all the way to 10, I have a post-accumulator for dividing (what is it called?) in the function that divides 10000 by 10 after it is called.

 

Any thoughts?

 

Edit: Actually since I don't know what a post dividing operator is I probably wont be able to use it.

Link to comment
Share on other sites

Link to post
Share on other sites

Hallelujah! I did it again with only 2 functions this time (technically 4, I had 1 for input, 1 for display), here is my code, please give me some feedback on it :)

//
//  main.c
//  Lab 9 Test
//

#include <stdio.h>

int input(int);
int div_func(int, int);
int mod_func(int, int);
void display(int, int, int, int, int);

int main()
{
    //Declaring input variable
    int a;

    //This variable will be used only in calculations
    int b = 10000;

    //Variables to store digits for displaying
    int idigit1, idigit2, idigit3, idigit4, idigit5, modres1, modres2, modres3, modres4;

    //Input
    a = input(a);

    //Calculations
    idigit1 = div_func(a, b);
    modres1 = mod_func(a, b);
    //Divide the value of b by 10 each time the line is written to make correct calculations
    b /= 10;

    idigit2 = div_func(modres1, b);
    modres2 = mod_func(modres1, b);
    b /= 10;

    idigit3 = div_func(modres2, b);
    modres3 = mod_func(modres2, b);
    b /= 10;

    idigit4 = div_func(modres3, b);
    modres4 = mod_func(modres3, b);
    b /= 10;

    idigit5 = div_func(modres4, b);

    display(idigit1, idigit2, idigit3, idigit4, idigit5);

    system("pause");
    return 0;
}

//Input
int input(int value)
{
    puts("Please input a value between 1 and 32767");
    scanf("%d", &value);
    return(value);
}

//Division function
int div_func(int a1, int b1)
{
    //Variables to store calculations
    int storage;
    //Calculation
    storage = a1 / b1;
    return (storage);
}

//Modulus Function
int mod_func(int a1, int mod_b)
{
    //Variables to store calculations
    int mod_storage;
    //Calculation
    mod_storage = a1 % mod_b;
    return (mod_storage);
}

//Display Function
void display(fdigit1, fdigit2, fdigit3, fdigit4, fdigit5)
{
    printf("\n\nThe digits spread out are:\n%d  %d  %d  %d  %d\n", fdigit1, fdigit2, fdigit3, fdigit4, fdigit5);
}

Edit: I have a line: system ("pause"); That's just a codeblocks thing don't worry about it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×