For each of the following, explain why it is not correct, and fix it. A. #includ
ID: 3755271 • Letter: F
Question
For each of the following, explain why it is not correct, and fix it.
A. #include<stdio.h>;
B. int function(void arg1){
return arg1-1;
}
C. #define MESSAGE=“Happynewyear!"
puts(MESSAGE)
================
4) Programming: Create the following three files:
1)main.c: main function that calls printStrings(int length) in myfunctions.c
2)myfunctions.c: has two functions
• printStrings: initializes a string s and call printStringsRec
• printStringsRec: a recursive function that prints all possible combinations of size ‘length’
as a parameter such that printed strings have no consecutive 1’s.
3)myfunctions.h: a header file to hold the two functions prototypes.
//Main.c
int main() {
int length;
scanf(“Enter string length: %d”,&length);
printStrings(length) ;
return 0;
}
For submission please write the run command used and the printed output in
addition to the code.
(In C programing language)
please help me with these 2 questions
thank you
Explanation / Answer
A. #include<stdio.h>;
Above will not give an compilation error, but it is not good practice to put semicolon at the end of the include statement in C.
Correct syntax: #include<stdio.h>
B. int function(void arg1) , this is wrong , we should use int as parameter of arg1 ,
Correct syntax is
int function(int arg1) {
return arg1 - 1;
}
C. #define MESSAGE=“Happynewyear!" , here MESSAGE = not allowed syntax shoukd be
#define MESSAGE "Happynewyear!"
puts(MESSAGE) , here semicolon missing
correct synytax is
puts(MESSAGE) ;
-------------------------------------------------------
//.h file declaration of two functions
void printStrings(int length);
void printStringsRec(char *S, int length);
-----------------------------------
//.c file for defintion of functions in .h file
void printStrings(int length)
{
char *s = (char*)malloc(length * sizeof(char));
/*printf("Enter numbers : ");
scanf_s("%s", s);*/
strcpy(s, "121131114");
printStringsRec(s, length);
}
void printStringsRec(char *s, int length)
{
int i = length-1;
if (length == 0)
return;
printf("String: ");
while (i >=0 )
{
if (s[i] == 49 )
{
if (s[i - 1] != 49)
printf("%c", s[i--]);
else
break;
}
else
printf("%c", s[i--]);
}
printf(" ");
printStringsRec(s, --length);
}
-------------------------------------------------
//main.cpp
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int length;
printf("Length: ");
scanf_s("%d", &length);
printStrings(length);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.