2019-05-30 12:17:34

I wanted to ask, is it possible to print patterns through functions in c?

In the case it is possible, then please tell me what mistake am I making in the program below. (for the first time perhaps, I am trying to create something without my teacher's asking.)

#include <stdio.h>
int pattern(int);
void main()
{
int n, r;
printf("Enter the value of n:");
scanf("%d",&n);
r=pattern(n);
printf("%d",r);
}
int pattern(int n)
{
int i, j;
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
return j;
}
}
}

Thanks.

2019-05-30 23:02:14

I think the problem might be that your trying to return multiple values with a single call one at a time. When you use the return function, it ends that function call. Full stop. So it'll likely only get through the first number, hit return, then exit the whole function instead of continuing the loop, and since you only call pattern() once, you likely only get one value. To fix this, you could make it so instead of returning the value right away, you could store it in a list, and then return the list when its finished to print it, or better still, print the value right there instead of returning it.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-06-02 15:27:44

@magurp:

From the list, I am asuming you are talking about arays?

Unfortunately, I have to restart with those, since dew to the exams my programming stopped for two months right where I was starting to learn arays.

"Print it right there," I am asuming again that you ment print from the function itself?

If yes, then would you mind telling me how can I do that? since the functions I have written so far and seen, do return something to the main(), and then the values are printed.

2019-06-03 08:16:10

Yes, an array is what I meant. Printing it in the function should be fairly straight forward, just replace return j; with printf("%d",j), like so:

#include <stdio.h>
int pattern(int);
void main()
{
int n, r;
printf("Enter the value of n:");
scanf("%d",&n);
pattern(n);
}
int pattern(int n)
{
int i, j;
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
printf("%d",j);
}
}
}
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-06-05 08:38:18

Hi everyone,
I'm also learning C language these days. I've almost completed the course and I have been given a few programs related to functions. I am not able to understand the logic of Fibonacci series, I know that I have to use recursion method, but what is the logic behind it?

Best regards
Pranam
Don't forget to give me a thumbs up!

2019-08-20 14:45:03

In the c primer+, there is a question about units of measurement, I have copied that question below:

In the U.S. system of volume measurements, a pint is 2 cups, a cup is 8 ounces, an ounce is 2 tablespoons, and a tablespoon is 3 teaspoons. Write a program that requests a volume in cups and that displays the equivalent volumes in pints, ounces, tablespoons, and teaspoons. Why does a floating-point type make more sense for this application than an integer type?

The float type does not present any problems for me. but I tried to search these volumes of measurements on the internet, and I did not get any clear answers so I could use those values in my program.

If you have knowledge of these measurements, please explain their values?

2019-08-20 15:05:06

you can find it here, under the heading called fluid volume
United States customary units

Paul

2019-08-20 15:21:18

It's simple, according to mathematics.
For example, if you got the measurement in cups, all you need to do to transform it in pinds is to divide it by 2 since a pind is specified as two cups. If you want to transform it further in ounces, you have to multiply  the cups by 8 since a ounce  is declared as eight cups.
I don't know whether those measurement units actually exist, but they could be fake for what I care, it would still be done the same regardless.

2019-08-20 15:45:51

Its just as easy as bgt lover suggests and its just plain mathematics. All you need to know is contained in the already mentioned sentences, you don't need to know more about the measurements, in fact, whatever the book says must not be true, but your answer would still be correct relating to the measurement the book offers. If you actually got the amount of cups entered, you could simply do the following:

float cups = 3.0; // you should make this value dynamic of course, so the user can input it
float pints = cups / 2; // printing it will yield approximately 1.5
float ounces = cups * 8; // result will be approximately 24 ounces
float tablespoons = ounces * 2; // approximately 48 tablespoons
float teaspoons = tablespoons * 3; // approximately 144 teaspoons

Best Regards.
Hijacker

2019-08-21 14:32:13

Thanks guys, now I can move on to next chapter in peace.

...until there comes a new problem.

2019-09-13 21:20:33 (edited by Dark Eagle 2019-09-13 21:22:09)

In the sums of loop problem of code abbey, you have to get the number of pairs from the user which they wish to sum, and then get them enter those pairs.

My problem is, the loop in my code does not end, and I don't know why. below is the code:

#include <stdio.h>
int main(void)
{
int i, n, j, sum1, sum2;
printf("Enter the pares of values to be summed:");
scanf("%d",&n);
for(i=1;i<=n;i++)//this loop is for the amounts of pairs.
{
for(j=1;j<=2;j++)//this loop is for getting the user to enter those pairs, and sum them.
{
printf("enter the pares of sums:");
scanf("%d%d",&sum1,&sum2);
printf("%d\n",sum1+sum2);
}
}
}

An example of problem:
suppose the user entered three, then the program would go like this:
Enter the pairs for sum:3
enter the pairs:1+2=3
2+9=11
10+20=30

I know that the inner loop does ends. because when I run the program, it asks for the pairs from start all over again. it is the outer loop which does not seems to end.

2019-09-13 21:37:46 (edited by pauliyobo 2019-09-13 21:39:40)

Hello.
You do not require the second loop
Since scanf will already ask you for input twice, creating a second loop will just encrease the times of this process. Therefore, if you for instance run the second loop  2 times, you will be asked for 4 numbers.
the code should be
#include <stdio.h>
int main(void)
{
int i, n, j, sum1, sum2;
printf("Enter the pares of values to be summed:");
scanf("%d",&n);
for(i=0;i<n;i++)//this loop is for the amounts of pairs.
{
printf("enter the pares of sums:");
scanf("%d%d",&sum1,&sum2);
printf("%d\n",sum1+sum2);
}
}
In this way the program should just ask you for 2 values and then encrease the step of the loop without doubling the values to ask.

Paul

2019-09-14 13:16:04

@Pauliyobo:
Thanks, I tried to change the value of variable i, just to check what happens. and apparently your loop would act the same way if the initial value is not zero.

You helped me a lot, thank you very much.

2019-09-18 08:46:05 (edited by Dark Eagle 2019-09-18 08:47:16)

Okay, a new problem.

This time, I have to compare the two numbers, and get the minimum of two as an answer.

For example, 3<5=3

But this time, the problem is, I also have negative numbers.

For example, -5<6=-5

Now, how can I compare negative numbers?

This time I don't have any code to put here either, sorry about that.

Please note that I do know how to do that with positive numbers, but negative numbers is something which I have not dealt with two often.

2019-09-18 13:48:46

Its just the same as before. As long as you're using a signed datatype like int, long or whatever instead of unsigned int, unsigned long (uint, ulong) etc. the well-known less than or greater than operations work fine.

int a = -256;
int b = a - 3;
if(b < a)
  printf("%d is smaller than %d", b, a);
else
  printf("%d is smaller than %d", a, b);

Will print:

-259 is smaller than -256

Best Regards.
Hijacker

2019-09-18 13:52:56

It is actually verry easy, the same as with positive numbers.
int compare(int a, int b)
a<b return a?return b;
Offcourse this bit of code may be wrong, especially my use of the turnary operator, but I wrote this code the way it is because I am on the phone and don't know where the braces are, but I think I should always provide a bit of code when answering such questions .

2019-09-18 16:12:06

That code isn't quite right, the tirnary comparison thing is an expression so you should return the whole thing, also it's written differently

int minimum(int a, int b) {
    return a < b ? a : b;
}

You don't need to do it this way, the more simple case is perfectly valid:

int minimum(int a, int b) {
    if (a < b) {
        return a;
    }
    return b;
}

Like someone said above, you can treat negative numbers just like positive numbers so long as the data type supports them (which int does, so long as it isn't an unsigned int).

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2019-09-18 16:34:10

I knew something wasn't quite right.
By the way, thank you for rembering me how to use that operator.
As I said above, I didn't write the code in a simpler way because of my phone's touchscreen keyboard.

2019-09-18 19:24:46

Thank you guise for your quick answers.

One last thing which I would like to know is that, if I get a user to enter the number, will scanf take the negative number?

feel free to rage if it is a stupid question.

2019-09-18 19:51:37

While I haven’t work with the language, if it already excepts normal Numbers, there is no reason that the method you are using should not accept negatives.

2019-09-18 19:55:43 (edited by stewie 2019-09-18 19:56:07)

Scanf does support negative number inputs. The same format specifiers (for the most part) that you'd pass to printf work for scanf in the same way. If you use %d (specifier for a normal signed int) it'll work.

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2019-09-19 00:37:06

I highly discourage use of the tirnary operator. It is trivial to make mistakes with it and makes your code harder to understand (especially with complex expressions). Just use normal if/else if/else/... as much as possible and avoid the tirnary.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-09-19 05:41:54

thanks for your pacients in explaining guise.

I tried the scanf situation by creating a small program. I actually was unsure regarding scanf taking negative numbers, because the input contains a character in the form of a dash. but all of my doubts are quelled.

@Ethin:
I actually myself don't like to use the ternary operator. ever since I learned it for the first time, it always has been a confusing thing to me.

All though, people at codewars do seem to like it a lot, at least from what I have observed.

2019-09-19 10:31:20

Ternary is absolutely fine in certain situations. You shouldn't use it in e.g. nested if situations, it will just mess things up, but in the above situation, where a single function should just return values depending on the input and there are just two outcomes possible, ternary is fine to use. If it starts to confuse you in that specific situation, you should go back and learn it again anyway.
Best Regards.
Hijacker

2019-10-05 15:24:25

So, I am trying to find the largest and the smallest value within an array. but the program spits out random values after the input is done.

Below is the source code, take a look at it, and please tell me what I am doing wrong.

#include <stdio.h>
int main(void)
{
int i, n;
long list[300], maximum, minimum;
printf("Enter a number between 1 to 300:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number %d",i+1);
scanf("%d",&list[ i ]);
}
for(i=1;i<n;i++)
{
if(list[0]<list[ i ])
{
maximum=list[0];
}
if(list[1]>list[ i ])
{
minimum=list[1];
}//if closed.
}//loop closed
printf("%d %d",maximum,minimum);
}