Evaluate a Recurrence Relation - c

A recurrence relation T is defined on n >= 0 and is given as T(n) = T(n-1) + 2*n with the base case T(0) = 1.
You will be given one integer k and you have to write a program to find out T(k).
The program must implement T( ) recursively.
Here is my Code :
include<stdio.h>
long long T(int input)
{
if(input == 0)
return 1;
return 2*input + T(input-1);
}
int main()
{
/*write your code here*/
int k;
scanf("%d",&k);
printf("%lli",T(k));
return 0;
}
Here is the Error i got
Program:1:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
Program:In function 'main':
Program:13:4: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
Program:13:4: warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]
Program:14:4: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
Program:14:4: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
Program:14:4: warning: implicit declaration of function 'T' [-Wimplicit-function-declaration]
Program:14:4: warning: format '%lli' expects argument of type 'long long int', but argument 2 has type 'int' [-Wformat]
How can i achieve the following Expected Results ?
Sample Test Cases
Input Output
Test Case 1 1 3
Test Case 2 2 7
Test Case 3 3 13

You are missing the square/hash/sharp # symbol behind the include on top.
Warnings indicate that scanf is not defined. Definition for that is given in stdio.h which you have failed to include.
Otherwise, your recursion function is pretty much alright.

You have a typo. Change
include<stdio.h>
to:
#include <stdio.h>

Related

flex bison windows introduction

As I'm new to lexer and parser, so I'm trying to read and understand others code.
Here is the code i'm trying to use : https://gist.github.com/justjkk/436828
But it's giving me error. How can I resolve this?
E:\flex_bison_test>gcc lex.yy.c y.tab.c -o json.exe
json.l: In function 'yylex':
json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l: In function 'strclone':
json.l:82:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
int len = strlen(str);
^~~~~~
json.l:82:15: warning: incompatible implicit declaration of built-in function 'strlen'
json.l:82:15: note: include '<string.h>' or provide a declaration of 'strlen'
json.l:79:1:
+#include <string.h>
%%
json.l:82:15:
int len = strlen(str);
^~~~~~
json.l:84:5: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]
strcpy(clone,str);
^~~~~~
json.l:84:5: warning: incompatible implicit declaration of built-in function 'strcpy'
json.l:84:5: note: include '<string.h>' or provide a declaration of 'strcpy'
y.tab.c: In function 'yyparse':
y.tab.c:627:16: warning: implicit declaration of function 'yylex' [-Wimplicit-function-declaration]
# define YYLEX yylex ()
^~~~~
y.tab.c:1272:16: note: in expansion of macro 'YYLEX'
yychar = YYLEX;
^~~~~
y.tab.c:1540:7: warning: implicit declaration of function 'yyerror'; did you mean 'yyerrok'? [-Wimplicit-function-declaration]
yyerror (YY_("syntax error"));
^~~~~~~
yyerrok
json.y: At top level:
json.y:80:6: warning: conflicting types for 'yyerror'
void yyerror (char const *s) {
^~~~~~~
y.tab.c:1540:7: note: previous implicit declaration of 'yyerror' was here
yyerror (YY_("syntax error"));
^~~~~~~
E:\flex_bison_test>
Or these should remain as it is.
All the commands, I have given :
flex json.l
bison -dy json.y
gcc lex.yy.c y.tab.c -o json.exe
Simply:
#include <string.h>
in your flex definitions section on top of json.l should fix it for you.
There's also a Makefile in the repository you pointed to. Maybe you should use that. You don't seem to be generating the parser files properly. See comment below.
As for the remaining warnings:
warning: implicit declaration of function 'yyerror';
warning: implicit declaration of function 'yylex';
These can be easily fixed by adding declarations of yylex() and yyerror should be present in the bison prologue section at the top of your json.y:
%{
int yylex();
void yyerror(const char *s);
%}
As for these ones:
json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
They're a bit more subtle. I would suggest have a look here on how to use yylval for correctly passing on strings from the lex's tokens into the parser's actions. The problem now is that yylval is a bare int but it ends up being assigned char pointers for both NUMBER and STRING tokens.

Error in using strcpy [duplicate]

This question already has answers here:
What header should I include for memcpy and realloc?
(2 answers)
warning: incompatible implicit declaration of built-in function 'strlen' and 'strcpy' [duplicate]
(1 answer)
Closed 5 years ago.
#include<stdio.h>
struct stud
{
char name[20];
int mark;
int per;
char grad[5];
};
void main(){
int i,n;
printf("Enter number of students");
scanf("%d",&n);
struct stud s[n];
for(i=1;i<=n;i++)
{
printf("Enter name of student");
scanf("%d",s[i].name);
printf("Enter obtained marks");
scanf("%d",&s[i].mark);
s[i].per=s[i].mark/5;
}
for(i=1;i<=n;i++)
{
if(s[i].per>=80)
strcpy(s[i].grad,"A");
else if(s[i].per>=60)
strcpy(s[i].grad,"B");
else if(s[i].per>=50)
strcpy(s[i].grad,"C");
else if(s[i].per>=40)
strcpy(s[i].grad,"D");
else
strcpy(s[i].grad,"F");
}
for(i=1;i<=n;i++)
{
printf("&s",s[i].name);
printf("&d",s[i].mark);
printf("&d",s[i].per);
printf("&s",s[i].grad);
}
}
When executing this code it's showing error :
main.c: In function ‘main’:
main.c:25:10: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
strcpy(s[i].grad,"A");
^~~~~~
main.c:25:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
main.c:25:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:27:11: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"B");
^~~~~~
main.c:27:11: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:29:11: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"C");
^~~~~~
main.c:29:11: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:31:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"D");
^~~~~~
main.c:31:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:33:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"F");
^~~~~~
main.c:33:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
What's the error here ?
scanf("%d",s[i].name); is undefined behavior. It will be "%s" format specifier.
And printf("%s",s[i].name) not &s.
Include string.h header file.
Also array index out of bound while getting input. Also undefined behavior. Array indices start from 0. All loop would be for(i=0;i<=n-1;i++)
and include header like this:
#include <string.h>
for(i=0;i<=n-1;i++)
{
printf("Enter name of student");
scanf("%s",s[i].name);
printf("Enter obtained marks");
scanf("%d",&s[i].mark);
s[i].per=s[i].mark/5;
}
Another change
for(i=1;i<=n;i++)
{
printf("%s",s[i].name);
printf("%d",s[i].mark);
printf("%d",s[i].per);
printf("%s",s[i].grad);
}

strtoul giving unexpected output

I am trying to use the strtoul function, but as shown below it is returning an unexpected value (adding ff's in the beginning):
#include <stdio.h>
#include <string.h>
#include <limits.h>
main() {
unsigned long temp ;
char *err;
temp = strtoul("3334444444",&err,10);
if (temp > UINT_MAX) {
printf("%lx %x %x\n",temp,3334444444,UINT_MAX);
}else
printf("%lx %x\n",temp,3334444444);
}
$./a.out
ffffffffc6bf959c c6bf959c ffffffff
The above output corresponds to the if part being true, though I expect the else part to get executed here. Can anyone please explain why strtoul is behaving like this? Why does it return ffffffffc6bf959c rather than just c6bf959c? If I use "333444444" (i.e. just one 4 less) rather than "3334444444" in the above code, then I get the correct output (i.e. 13dff55c 13dff55c) corresponding to the else part.
Note : As pointed by melpomene in his reply below, stdlib.h header file should have been included and that will resolve the issue. Can anyone please let me know what is being done by the program by assuming the incorrect return type (int in this case) during compile time which can't be undone (or atleast it is not getting undone in this case) even after knowing the correct return type (unsigned long in this case) during link time ? In short, i want to know how c6bf959c is getting converted to ffffffffc6bf959c because of prototype not provided.
Compiling your code with gcc with warnings enabled gives:
try.c:5:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
^~~~
try.c:5:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
try.c: In function ‘main’:
try.c:8:12: warning: implicit declaration of function ‘strtoul’ [-Wimplicit-function-declaration]
temp = strtoul("3334444444",&err,10);
^~~~~~~
try.c:8:5: warning: nested extern declaration of ‘strtoul’ [-Wnested-externs]
temp = strtoul("3334444444",&err,10);
^~~~
try.c:10:22: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘long long int’ [-Wformat=]
printf("%lx %x %x\n",temp,3334444444,UINT_MAX);
^
try.c:12:22: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘long long int’ [-Wformat=]
printf("%lx %x\n",temp,3334444444);
^
The main problem is implicit declaration of function ‘strtoul’, indicating that the function is not declared (and thus assumed to return int) because you forgot to #include <stdlib.h>. Adding the missing #include fixes the value of temp.
But you should also have a look at the warnings reported for printf and fix those.

Is there anything that i missed in signed number in C language?

This is my basic C test program.
After I built it, I just entered negative number like -1, -2, etc. in console.
But the result is "oh", not "another number".
I don't know why this happens, because negative numbers should make the 'if' statement true.
int main(int argc, char* argv[]){
long int num;
scanf("%d", &num);
if(num ==1 || num < 0){
printf("another number\n");
}else{
printf("oh\n");
}
}
When you use the %d format string with scanf, the corresponding argument will be treated as int*. But you have passed a long int*. The value scanf stores will not be the same size as what your if statement reads.
Formally, you get undefined behavior. In practice, on most platforms scanf will write only part of the variable, and the rest will be left with an arbitrary value, with the usual bad effects on future use.
Use %ld for long variables, %d for ints. Change your code to one of these:
int num;
scanf("%d", &num);
or
long int num;
scanf("%ld", &num);
/tmp$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:4:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
foo.c:4:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
foo.c:4:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long int *’ [-Wformat]
foo.c:7:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
foo.c:7:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:9:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
fix the causes of those warnings and all the bugs will go away.

Weird Compiler Error in Parser Code

Parser.h
enum { PLUS, MINUS, DIVIDE, MULTIPLY, NUMBER, END } type;
int token;
/* parsing functions */
void parse_token (void);
Parser.c
void get_token (void)
{
token++;
parse_token(); /* LINE 11 */
}
void parse_token (void) /* LINE 14 */
{
if ( strchr ("1234567890.", token) )
type = NUMBER;
else if ( strchr ("+", token) )
type = PLUS;
else if ( strchr ("-", token) )
type = MINUS;
else if ( strchr ("/", token) )
type = DIVIDE;
else if ( strchr ("*",token) )
type = MULTIPLY;
else if ( token == '\0' )
type = END;
else
show_error(strcat("Couldn't parse token : ", token));
}
The Errors
parser.c:14:6: warning: conflicting types for ‘parse_token’ [enabled by default]
parser.c:11:2: note: previous implicit declaration of ‘parse_token’ was here
parser.c: In function ‘parse_token’:
parser.c:16:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:17:3: error: ‘type’ undeclared (first use in this function)
parser.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
parser.c:17:10: error: ‘NUMBER’ undeclared (first use in this function)
parser.c:19:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:20:10: error: ‘PLUS’ undeclared (first use in this function)
parser.c:22:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:23:10: error: ‘MINUS’ undeclared (first use in this function)
parser.c:25:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:26:10: error: ‘DIVIDE’ undeclared (first use in this function)
parser.c:28:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:29:10: error: ‘MULTIPLY’ undeclared (first use in this function)
parser.c:32:10: error: ‘END’ undeclared (first use in this function)
parser.c: In function ‘show_error’:
parser.c:40:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
I'm utterly bamboozled. :(.
Any help?
One you get it to compile (by including the header, as Luchian Grigore said), you'll find that you can't do strcat() on a constant string.
The constant string is allocated in read-only memory, and can't be modified. And even if you could modify it, you would be overwriting other things in memory.
You're not including your header, so there's no way for the translation unit to know about the declarations of type and token.
You need:
#include "Parser.h"
at the beginning of the implementation file.

Resources