/*Program to print all the numbers between a lower bound and a upper bound values*/
#include<stdio.h>
#include<stdlib.h>
void recur(int a, int b);
int main(void)
{
int x,y;
printf("Enter the lower and upper bound values: \n");
scanf("%d %d",&x,&y);
void recur(x,y);
return 0;
}
void recur(int a,int b)
{
if(a<b)
{
printf("%d /n",a);
a++;
void recur(a,b);
}
}
The output I get is:
Enter the lower and upper bound values:
10
50
process returned 0.
Is there anything wrong with the syntax or return type..?
I've just started learning c.Need Help
Both
void recur(x,y);
void recur(a,b);
declares the functions (prototype). To call them, change them to
recur(x,y);
recur(a,b);
void recur(int a,int b)
{
if(a<b)
{
printf("%d /n",a);
a++;
//void recur(a,b); You need to call the function not to declare
// there is difference between dec and calling.
recur(a,b); // is the correct way to do this .
}
}
same requires in main() method though
You have the following errors in your program:
When you call a function the syntax is
funcName(param1, param2);
or, if function returns a value:
ret = funcName2(param3, param4, param5);
In your code, putting void at call time is a syntax error:
void recur(a,b);
This is how function is declared, not called. Attention there is a difference between function declaration and function definition.
If you want to print special chars in C, like newline, you need to print '\n' line this:
printf("some message followed by newline \n");
Note that '\n' is a single char, even if you see a '\' and a 'n'. '\' has the purpose to escape the next char 'n' making it a new char. So '\n' is the newline character.
Other special characters in C are: '\t' for tab, '\' to actually print a '\'. Strings in C are enclosed in double quotes like "message" while chars are enclosed in single quotes like 'a', 'b', '\n', 'n', '\'.
Here is your working code.
#include<stdio.h>
#include<stdlib.h>
void recur(int a, int b);
int main(void)
{
int x,y;
printf("Enter the lower and upper bound values: \n");
scanf("%d %d",&x,&y);
recur(x,y); // change made here
return 0;
}
void recur(int a,int b)
{
if(a<b)
{
printf("%d \n",a);
a++;
recur(a,b); // change made here
}
}
Related
I am working on an exercise in cs50 pset2 readability, and I'm trying to count up how many sentences there are in a text. So far my code is this:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int isdelim(char c, int sentences)
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
for(int i = 0; i < strlen(text);i++)
{
sentences = isdelim(text[i], sentences);
}
printf("Sentences: %i\n", sentences);
}
int isdelim(char c, int sentences)
{
return c == '.' || c == '!' || c == '?';
sentences++;
}
But when I do ./readability and I type the text: Hello, world! Beautiful day!, it gives an output of: Sentences:0.
I am new to C so I can't really understand all these functions. I tried searching the web and using other external sources, but nothing makes sense. If you show me the answer, please make sure to tell me why it works, so I can learn. I may need the knowledge in the future. Thanks a lot.
There's several problems.
When you execute a return statement, the function ends. Nothing after that will be executed. So sentences++ is never executed.
In C, function parameters are passed by value. So incrementing sentences in the isdelim() function will not affect the variable in main().
int delim(char c, int sentences); is a function declaration, not a function call. Function calls don't include the type declarations.
You can solve the first problem by using an if statement that checks the condition, rather than returning the result of the condition immediately.
You can solve the second problem by passing a pointer to the variable and dereferencing it, or by having the function return the new value of sentences.
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int isdelim(char c, int sentences);
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
for(int i = 0; i < strlen(text);i++)
{
sentences = isdelim(text[i], sentences);
}
printf("Sentences: %i\n", sentences);
}
int isdelim(char c, int sentences)
{
if (c == '.' || c == '!' || c == '?') {
sentences++;
}
return sentences;
}
#Baramar makes some good points but I think the code could use a refactoring. The isdelim function is confusing. It's named as a Boolean function, so it should return a bool. And that function shouldn't take a sentences parameter; rather, let the caller handle the incrementing. And I've taken the liberty to call strlen before the loop so it doesn't get recomputed on each iteration. So:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
bool isdelim(char c);
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
int length = strlen(text);
for (int i=0; i<length; i++)
{
if (isdelim(text[i]))
{
sentences++;
}
}
printf("Sentences: %i\n", sentences);
}
bool isdelim(char c)
{
return c == '.' || c == '!' || c == '?';
}
I'm having trouble calling the function more than one time in my C program. The gist of the assignment is to replace the whitespaces in a sentence inputted from the user and replace it with a different character. For some reason, the program will call the same first function multiple times. I tried putting the strlen(x) in a variable inside my function, but I'm not very well versed in the C language, so I decided to leave it out of my code.
#include <string.h>
void display(char x[], char y);
void main(){
//Do not change this function
char a[100];
printf("Enter a sentence\n");
gets(a);
display(a, '*'); //To replace every space by *
display(a, '-'); //To replace every space by -
display(a, '+'); //To replace every space by +
}
void display(char x[], char y){
for(char i = 0; i < strlen(x); i++) {
if(x[i] == ' ') {
x[i] = y;
}
}
printf("%s\n", x);
}
It does not "call the same first function". You change the value of your string inside the function, so after the first run of the function the string does not have spaces. Therefore the second and third call print the string unchanged:
void display(char x[], char y){
for(char i = 0; i < strlen(x); i++) {
if(x[i] == ' ') {
// this happens only upon first call!
x[i] = y;
}
}
printf("%s\n", x);
}
Edit: to fix the issue, for example see the comment Ring Ø added and follow the advice
I am quite newbie in c, so I just starting off with some code, experimenting some stuff, right now I am stuck with this problem in C, creating a function that displays the alphabet in lowercase, on a single line, by ascending order, starting from the letter ’a’.
This is where I am stuck:
#include <stdio.h>
int alfabet(unsigned int i) {
if(i <= 122) {
char litera = i;
return litera;
}
return alfabet(i+1);
}
int main() {
int i = 97;
printf(alfabet(i));
return 0;
}
Here, you won't print anything really interesting. In fact, your application will crash because printf() require at least a char * parameter (a string).
Your alfabet() function seems not so bad, but you should print the letter in it :
int alfabet(unsigned int i)
{
if (i > 'z') {
// Here is the stop condition.
// If the value is higher than 122 ('z' character), we stop recursivity)
return;
}
printf("%c ", i);
// Otherwise, let's call this function with another character
return alfabet(i+1);
}
Target simplicity
void alfabet(int c) {
printf("%c", c);
if (c < 'z') alfabet(c+1);
}
called from main as
alfabet('a');
You may add a printf("\n");
the function prints the character given as parameter
you only call recursively the function with the next character to be printed if necessary, i.e. if the current character is below z.
Something like that:
#include <stdio.h>
void alfabet(char i) {
if(i < 'z')
{
alfabet(i+1);
}
printf("%c", i);
}
int main() {
char i = 'a';
alfabet(i);
return 0;
}
to print zyxwvutsrqponmlkjihgfedcba. Or:
#include <stdio.h>
void alfabet(char i) {
printf("%c", i);
if(i < 'z')
{
alfabet(i+1);
}
}
int main() {
char i = 'a';
alfabet(i);
return 0;
}
to print abcdefghijklmnopqrstuvwxyz
As you are new in this language, the basic thing to know is that each and every character on the keyboard has its own ASCII value ranging from 000 to 127 (i.e. total 128).
Now if you want to print a to z in a single line, the ASCII value for 'a' is 97 and that for 'z' is 122.
So, for printing this on screen you need to learn the basic for loop structure.The syntax for basic for loop is as follows :-
for(expr1;expr2;expr3)
{
Body of the loop;
}
Here, expr1 refers to the initial value of the variable, expr2 refers to the exit condition of the loop and expr3 refers to the increment or decrement value.
So, the code to print a to z is as follows :-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
print_alpha();
getch();
}
void print_alpha()
{
int i;
for(i=97;i<+122;i++)
{
printf("%c",i);
}
}
int main() {
int i = 97;
printf("%c",alfabet(i));
return 0;
}
You Have to provide format specifier in printf (i.e. %c for character, %d for integer) Just check man printf in terminal.
And for printing a to z you can use for loop suggested by #Michel Jord, for printing in one line just put space in place of \n
#include <stdio.h>
void print_alphabets(char i) {
if(i>='a' && i<='z')
{
print_alphabets(i+1);
printf("%c ", i);
}
}
int main() {
char i;
scanf("%c",&i);
print_alphabets (i);
return 0;
}
I need to add two numbers. The constraint here is that i also have to check that the input is not an alphabet. I have used isdigit() function to check for this. But when i run the code it executes the else block even if the input is number.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int a,b,c;
clrscr();
scanf("%d\n",&a);
scanf("%d",&b);
c=a+b;
if(isdigit(a) && isdigit(b))
{
printf("%d",c);
printf("\n");
}
else
{
printf("Inavild Output");
}
getch();
}
The function isdigit expect an integer representation of a character (it's ascii value). You are giving real integer values, whose value probably is not a digit when checking the ascii table.
First, if you want to take inputs from the user you would need to use char or string variable. It is much clearer. Like :
char input_a;
char input_b;
Secondly, you cannot add char values directly if what you want is the result of adding the digit not adding the Ascii representation for them. So the line c=a+b would need to be done after you have ensured input_a and input_b were in fact digits.
Thirdly, you need to convert input_a and input_b to the value they represent. So your "if" would look like this (note there are many ways to convert a char into an int) :
if(isdigit(input_a) && isdigit(input_b))
{
int a = atoi(input_a);
int b = atoi(input_b);
int c = a + b;
printf("%d",c);
printf("\n");
}
int isdigit(int argument) take integer argument as a parameter and check the
argument is a digit or not if it is a digit then return 1(true) if
the argument is not a digit then it return 0(false).
if you pass int a = 65 to the function it will convert the value to
it's equivalent ASCII Value and return the result false(0). Because
65 is equivalent to 'A' and 'A' is not a digit.
You can check it this way.if(scanf("%d",&a) == 1 && scanf("%d",&b) == 1). If the input is number then calculate and print the result, if input is not number than it will print Inavild Output.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
if(scanf("%d",&a) == 1 && scanf("%d",&b) == 1) //if numbers read condition will be true
{
c=a+b;
printf("%d",c);
printf("\n");
}
else
{
printf("Inavild Output");
}
getch();
}
I have been reading "The C Programming Language" and I got to this problem, that my output is 0 for any given string I send.
My function looks like this:
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word);
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
Problem:
I send him word for example: Jhonny, and character n, so it should count number of n's in the word (in this case the output should be 2).
What am I doing wrong?
#include <stdio.h>
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word); //<------- You need to remove this one because it may overwrite
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
int main(void) {
// your code goes here
printf("%d",number_of_repeating("johnny",'n'));
return 0;
}
if you're passing the string in there is no reason to call gets(), that could be it or your types could be wrong.