WSL(ubuntu) printf only outputs the same text - c

I have a problem. My code:
#include <stdio.h>
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
printf("variable 2 :");
scanf("%d", &y);
return y;
}
int compare (int, int);
int main() {
int x = readInteger(x);
int y = readInteger(y);
printf("%d is greater", compare(x,y));
return 0;
}
int compare(int x , int y) {
if(x > y) return x;
else return y;
}
only outputs
variable 1: ...
variable 1: ...
instead of
variable 1: ...
variable 2: ...

The issue is because return means "end function execution here". So, you have
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
printf("variable 2 :");
scanf("%d", &y);
return y;
}
which, because of the return x line, is equivalent to
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
}
One quick fix is to remove return x; line. Though then it won't do exactly what you want the function to do, since you want to read 2 integers and return them.
A better fix is to pass the string to the function as argument and then call it twice:
int readInteger(const char* prompt) {
int x;
printf(prompt);
scanf("%d", &x);
return x;
}
int main() {
int x = readInteger("variable 1 :");
int y = readInteger("variable 2 :");
printf("%d is greater", compare(x,y));
return 0;
}
PS. If a function has no arguments (readInteger in your original code, you should not call it with arguments (int x = readInteger(x) in main).

Related

Function to find odd numbers returns extra value - C Language

I wrote a code in C to find the odd numbers from a given interval of min and max number. The function works well when it is inside the int main() but not well when outside the program as a function.
What's more is that it also prints the incremented number outside the max number given.
This is the code...
#include <stdio.h>
// My Function
int odd_numbers(int x, int y) {
for (int i = x; i <= y; ++i) {
if (i % 2 == 1) {
printf("%d\n",i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
printf("%d",odd_numbers(min_num,max_num));
}
and this is the output...
As you can see, it adds an 11 besides the 9...
How can I solve this? I've tried return 0; and it returns the value 0 but i only want to return no number except the odd numbers.
Here is the working code.
Notes
Change the return type of odd_numbers from int to void because you are not returning anything when the function is called.
Only call the function odd_numbers, no need to printf anything because odd_numbers already does the job.
#include <stdio.h>
// My Function
void odd_numbers(int x, int y) {
for (int i = x; i <= y; i++) {
if (i % 2 != 0) {
printf("\n%d",i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
odd_numbers(min_num,max_num);
}
Here is the modified code.
you have declare function return type int but return nothing. odd_numbers made to void type. no need to return anything
code:
#include <stdio.h>
// My Function
void odd_numbers(int x, int y)
{
int i = 0;
for (int i = x; i <= y; i++)
{
if (i % 2 != 0)
{
printf("%d\n", i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
odd_numbers(min_num, max_num);
return 0;
}

Swap number bug

I wrote a simple program that accepts 2 number to be swap in swapNum function however, it returns some numbers I don't understand. Can someone explain how this happened and suggests better.
#include <stdio.h>
int swapNum(int num1, int num2){
int tempNumber;
tempNumber = num1;
num1 = num2;
num2 = tempNumber;
return num1;
return num2;
}
int main(){
int firstNum, secondNum;
printf("Enter the first number:");
scanf("%i", &firstNum);
printf("Enter the second number:");
scanf("%i", &secondNum);
printf("Swapped value: first number: %i, second number: %i", swapNum(firstNum,secondNum));
return 0;
}
Here is the image of the result. The bug is at the last line at second number.
As others have pointed out, you can only have one return statement from a function and thus only return on parameter (although it can be a structure).
In this instance I would pass the integer values into the function by using integer pointers and passing their address e.g.:
int swapNum(int *num1, int *num2);
int main()
{
int a = 2;
int b = 3;
printf("a=%d b=%d\n",a,b);
swapNum(&a, &b);
printf("a=%d b=%d\n",a,b);
return 0;
}
int swapNum(int *num1, int *num2)
{
int tempNumber;
tempNumber = *num1;
*num1 = *num2;
*num2 = tempNumber;
return 0;
}
Output
a=2 b=3
a=3 b=2

How to add a counter of a for loop and pass it across functions in C

I am relatively new to programming. I have been working on a project that takes the users input passes it to a function that finds the factors of the number, stores them in an array (only for 100 factors max) and then returns the array to the main program. Once I call the array to the main program, then print it using a for loop i run into an issue. I need a counter to figure out the size of the array to figure out how many times to run the for loop.
So far I have tried to use a pointer to count the first loop and send it to the main function with no luck. The only thing that works is a global variable, but for obvious reasons I would prefer not to do that.
#include <stdio.h>
#define MAX_SIZE 100
int* get_factors(int number)
{
int x;
int z;
int y = 0;
static int arr[MAX_SIZE];
for(x=1; x <= number; ++x)
{
if (number%x == 0)
{
arr[y] = x;
y++;
}
}
return arr;
}
int main(void)
{
int *num;
int temp;
int z = 0;
printf("Enter a number: ");
scanf("%d",&temp);
printf("Factors of %d: ", temp);
num = get_factors(temp);
for(z=0; z<=20; z++) //This is the loop that i need to figure out a
counter for
{
printf("%d ", num[z]);
}
return 0;
}
#include <stdio.h>
#define MAX_SIZE 100
struct myObject {
int arr[MAX_SIZE];
int index;
};
struct myObject
get_factors (int number)
{
int x;
struct myObject holder;
holder.index = 0;
for (x = 1; x <= number; ++x)
{
if (number % x == 0)
{
holder.arr[holder.index] = x;
holder.index++;
}
}
return holder;
}
int
main (void)
{
struct myObject num;
int temp;
int z;
printf ("Enter a number: ");
scanf ("%d", &temp);
printf ("Factors of %d: ", temp);
num = get_factors (temp);
for (z = 0; z < num.index; z++) //This is the loop that i need to figure out a
//counter for
{
printf ("%d ", num.arr[z]);
}
return 0;
}
If you change your function interface design, it could be easier to pass that information.
The idiomatic way to pass array in C function is via parameter, where the first paramenter is the array itself, and the second parameter is it maximun length.
In this way your function is not more responsible for allocating or deallocating the array. In other words, it does not own it anymore.
In your case, you can use the function return to pass the amount of slots used in this array and, on error, just a negative number.
In this way, your program can be something like that:
int get_factors(int number, int number[], size_t length)
{
int x;
int z;
int y = 0;
if ( number > length ) return -1;
for(x=1; x <= number; ++x)
{
if (number%x == 0)
{
arr[y] = x;
y++;
}
}
return y;
}
and at call site:
int main(void) {
int num[MAX_SIZE];
int temp;
int z = 0;
printf("Enter a number: ");
scanf("%d",&temp);
printf("Factors of %d: ", temp);
int slots = get_factors(temp, num, MAX_SIZE);
for(z=0; z<=slots; z++) //This is the loop that i need to figure out a
printf("%d ", num[z]);
return 0;
}

Testing recursive functions - C

I have a small problem on my homework here
I just need to write a recursive function to test this relation
f(x,y)=x if y==1
otherwise
f(x,y) = x + f(x,y-1)
Here is my source, I can't get it to print out correctly.
Note that I have the user enter X and Y to test.
#include <stdio.h>
int f (int x,int y);
int main (void)
{
int x, y, z;
printf ("\nEnter x: ");
scanf ("%d", &x);
printf ("\nEnter y: ");
scanf ("%d", &y);
x=f(x,y);
return 0;
}
int f (int x,int y)
{
if (y==1)
{
return x;
}
else
{
return (x + f(x,y-1));
}
}
Your recursive function should look like:
#include<stdio.h>
int main() {
int i = f(5,4);
printf("%d\n",i);
return 0;
}
int f (int x,int y)
{
if (y==1)
{
return x;
}
else
{
return (x + f(x,y-1));
}
}

Checking For user input between certain limit if not asking again until correct input is entered

My program compiles correctly but i am having problem when i run it. The first scanf (width) works correctly but when i try with another scanf(height) i get segmentation fault 11.
And can i do this program to work without using pointers. (Also i need limit checker function because i have to use it again and again in my program).
#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
int* x;
int* y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", x);
limitChecker(3, 5, x);
printf("width: %d \n", *x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", y);
limitChecker(2, 4, y);
printf("Height: %d \n", *y);
}
void limitChecker(int x, int y, int* input)
{
while(!(*input>=x && *input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", input);
}
}
You need to use a reference to the variables used in the scanf().
For example, scanf("%d", &x);
The first parameter of scanf() is for the type of data, and the following parameter(s) are a list of pointers to where you would like the user input to be stored.
CORRECTED CODE:
#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
int x;
int y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", &x);
limitChecker(3, 5, &x);
printf("width: %d \n", x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", &y);
limitChecker(2, 4, &y);
printf("Height: %d \n", y);
}
void limitChecker(int x, int y, int* input)
{
while(!(*input>=x && *input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", input);
}
}
You did not allocate memory to hold x and y.
Allocate them on the stack and then use the & address of operator to obtain a pointer to that memory.
#include <stdio.h>
int limitChecker(int x, int y, int input);
int main(void)
{
int x;
int y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", &x);
x = limitChecker(3, 5, x);
printf("width: %d \n", x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", &y);
y = limitChecker(2, 4, y);
printf("Height: %d \n", y);
}
int limitChecker(int x, int y, int input)
{
while(!(input>=x && input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", &input);
}
return input;
}
If you want x and y to be pointers then you have to assign them valid memory before you use them.
int * x = malloc(sizeof(int));
int * y = malloc(sizeof(int));
#include <stdio.h>
int limitChecker(int x, int y, int value){
return x <= value && value <= y;
}
int inputInt(void){
//x >= 0
int x = 0;
int ch;
while('\n'!=(ch=getchar())){
if('0'<=ch && ch <= '9')
x = x * 10 + (ch - '0');
else
break;
}
return x;
}
int main(void){
int x, y;
do{
printf("Enter the width of the windows. (3 - 5) : ");
x = inputInt();
}while(!limitChecker(3, 5, x));
printf("width: %d \n", x);
do{
printf("Enter the height of the windows. (2 - 4) : ");
y = inputInt();
}while(!limitChecker(2, 4, y));
printf("Height: %d \n", y);
return 0;
}

Resources