This is my code the only thing I can do is quit. There are two functions one where it calculates Fahrenheit to Celsius and the other does the opposite and there is an array that stores the data as well. Then I added the switch at the end. I understand each concept individually but don't know how to bring them together.
#include <stdlib.h>
#define SIZE 1
//C = (5 / 9) * (F - 32) and F = (9 / 5) * C + 32
// calculate f to c
int getValue() {
int result;
printf("Input degree: ");
scanf_s("%i", &result);
return result;
}
int F (int input[], int size) {
int result = 0;
int a;
for (a = 0; a = size; a++) {
if (input[a] = result);
result = (9 / 5) * input[a] + 32;
}
return result;
}
int C (int input[], int size) {
int result = 0;
int a;
for (a = 0; a = size; a++) {
if (input[a] = result);
result = (5 / 9) * (input[a] - 32);
}
return result;
}
main() {
int choice;
int input[SIZE];
int F, C;
int a;
for (a = 0; a < SIZE; a++) {
input[a] = getValue();
F = (input, SIZE);
C = (input, SIZE);
}
do {
printf("Welcome to the Main Menu\n");
printf("1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius\n");
printf("2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit\n");
printf("3. Quit.\n");
scanf_s("%i", &choice);
} while (choice != 3);
switch (choice) {
case 1:
printf("%i degree C\n", F);
break;
case 2:
printf(" % i degree F\n", C);
break;
case 3:
printf("You have chosen option 3 you are now able to quit\n");
break;
}
system("pause");
}```
Beside the errors pointed in the answer by #Mehmet Aslan, you should move the switch block into the while loop, so it gets executed each time the user selects an option. As it is now, it will only be run once, when the user has chosen the option "3"
Since this looks like homework here is constructive feedbacks. Hopefully you ll manage.
Hints:
Look into pointers
int getValue();
void F(int *output, int *input, int size);
void C(int *output, int *input, int size)
{
for(int i = 0; i < size; ++i)
{
output[i] = (5 / 9) * (input[i]- 32);
}
}
#include <stdlib.h>
#define SIZE 1
//C = (5 / 9) * (F - 32) and F = (9 / 5) * C + 32
// calculate f to c
int getValue() {
int result;
printf("Input degree: ");
scanf_s("%i", &result);
return result;
}
int F (int input[], int size) {
int result = 0;
int a;
for (a = 0; a = size; a++) {
if (input[a] = result);
result = (9 / 5) * input[a] + 32;
}
return result;
}
int C (int input[], int size) {
int result = 0;
int a;
for (a = 0; a = size; a++) {
if (input[a] = result);
result = (5 / 9) * (input[a] - 32);
}
return result;
}
main() {
int choice;
int input[SIZE];
int F, C;
int a;
for (a = 0; a < SIZE; a++) {
input[a] = getValue();
F = (input, SIZE); <<---compile time error, not a function call
C = (input, SIZE); <<---compile time error, not a function call
}
// F(output, input, SIZE) <<--- it should be here, so you could calculate after collect inputs
do {
printf("Welcome to the Main Menu\n");
printf("1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius\n");
printf("2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit\n");
printf("3. Quit.\n");
scanf_s("%i", &choice);
} while (choice != 3);
switch (choice) {
case 1:
printf("%i degree C\n", F); <<---F compile time error, not a function call
break;
case 2:
printf(" % i degree F\n", C); <<---C compile time error, not a function call
break;
case 3:
printf("You have chosen option 3 you are now able to quit\n");
break;
}
system("pause");
}
hello sorry in advance because i ask a very simple question, but i really ask for help, i have a school assignment, but i don't know how to solve it, please help
Stimulation
After you study program modularization, does the if else program below contain the process of modularization?
#include <stdio.h>
main(){
int a,b,c, max;
printf("enter value a :");scanf("%d",&a);
printf("enter value b :");scanf("%d",&b);
printf("enter value c :");scanf("%d",&c);
//checking 1 program module
if((a>b)&&(a>c))
{
max = a;
printf("highest value checking 1 : %d" ,max);
}else if(b > c)
{
max=b;
printf("highest value checking 1 : %d" ,max);
}else{
max=c;
printf("highest value checking 1 : %d" ,max);
}
printf("\n");
int number1, number2, number3;
number1=a;
number2=b;
number3=c;
//checking 2 main programs
if((number1>number2)&&(number2>number3)){
max = number1;
printf("highest score checking 2 : %d" ,max);
}else if(number2 > number3)
{
max=number2;
printf("highest score checking 2 : %d" , max);
}else{
max=number3;
printf(highest score checking 2 : %d" ,max);
}
}
Identification of problems.
Write down the hypothesis from the results of the if else program study. Is the hypothesis alternative (the if else program line is in accordance with the concept of modularization) or the hypothesis is a null hypothesis (the if else program is not in accordance with the concept of modularization). Explain the hypothesis in detail, and point out which line of if else program strengthens your hypothesis statement!
The program is not modular.
Here is the analysis:
The two if/else blocks are nearly identical.
The printf statements are replicated code.
The scanf calls are replicated code.
The code can only handle three numbers.
Amongst other things, modularization is a process of eliminating replicated/duplicated code that is similar and placing the common code in [smaller] functions.
A good maxim to follow is that a given function: should do one thing well.
We can modularize the code in several steps:
Move the first if/else block into a separate function (e.g. getmax)
Eliminate duplicate printf in getmax
Make getmax more general so it can handle the second if/else more easily
Convert second if/else block in main to use getmax
Put user input (printf/scanf) into common function
Convert program to use arrays rather than individual scalars. This is particularly evident with: int number1,number2,number3;. It can be simplified into: int number[3];
Convert program to prompt user for number of elements in the array
Move the first if/else block into a separate function:
#include <stdio.h>
// checking 1 program module
int
getmax(int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
printf("highest value checking 1 : %d\n", max);
}
else if (b > c) {
max = b;
printf("highest value checking 1 : %d\n", max);
}
else {
max = c;
printf("highest value checking 1 : %d\n", max);
}
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax(a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
if ((number1 > number2) && (number2 > number3)) {
max = number1;
printf("highest score checking 2 : %d\n", max);
}
else if (number2 > number3) {
max = number2;
printf("highest score checking 2 : %d\n", max);
}
else {
max = number3;
printf("highest score checking 2 : %d\n", max);
}
return 0;
}
Eliminate duplicate printf in getmax:
#include <stdio.h>
// checking 1 program module
int
getmax(int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest value checking 1 : %d\n", max);
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax(a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
if ((number1 > number2) && (number2 > number3)) {
max = number1;
printf("highest score checking 2 : %d\n", max);
}
else if (number2 > number3) {
max = number2;
printf("highest score checking 2 : %d\n", max);
}
else {
max = number3;
printf("highest score checking 2 : %d\n", max);
}
return 0;
}
Make getmax more general so it can handle the second if/else more easily:
#include <stdio.h>
// checking 1 program module
int
getmax(const char *what,int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest %s checking 1 : %d\n", what, max);
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax("value",a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
if ((number1 > number2) && (number2 > number3)) {
max = number1;
printf("highest score checking 2 : %d\n", max);
}
else if (number2 > number3) {
max = number2;
printf("highest score checking 2 : %d\n", max);
}
else {
max = number3;
printf("highest score checking 2 : %d\n", max);
}
return 0;
}
Convert second if/else block in main to use getmax:
#include <stdio.h>
// checking 1 program module
int
getmax(const char *what,int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest %s checking 1 : %d\n", what, max);
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax("value",a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
getmax("score",number1,number2,number3);
return 0;
}
Step 6: Put user input into common function:
#include <stdio.h>
// ask user for number
int
asknum(const char *prompt)
{
char buf[1000];
int num;
printf("enter value %s : ",prompt);
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);
return num;
}
// checking 1 program module
int
getmax(const char *what,int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest %s checking 1 : %d\n", what, max);
return max;
}
int
main(void)
{
int a, b, c, max;
a = asknum("a");
b = asknum("b");
c = asknum("c");
getmax("value",a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
getmax("score",number1,number2,number3);
return 0;
}
Convert program to use arrays rather than individual scalars:
#include <stdio.h>
// ask user for number
int
asknum(int prompt)
{
char buf[1000];
int num;
printf("enter value %d : ",prompt);
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);
return num;
}
// checking 1 program module
int
getmax(const char *what,const int *arr,int count)
{
int idx;
int val;
int max = -1;
if (count > 0) {
max = arr[0];
for (idx = 1; idx < count; ++idx) {
val = arr[idx];
if (val > max)
max = val;
}
printf("highest %s checking 1 : %d\n", what, max);
}
return max;
}
int
main(void)
{
int idx;
int count = 3;
int arr[count];
for (idx = 0; idx < count; ++idx)
arr[idx] = asknum(idx);
getmax("value",arr,count);
int number[count];
for (idx = 0; idx < count; ++idx)
number[idx] = arr[idx];
//checking 2 main programs
getmax("score",number,count);
return 0;
}
Convert program to prompt user for number of elements in the array
#include <stdio.h>
// ask user for number
int
asknum(const char *prompt)
{
char buf[1000];
int num;
printf("%s : ",prompt);
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);
return num;
}
// ask user for array value
int
askval(int idx)
{
char prompt[100];
int num;
sprintf(prompt,"Enter array value %d",idx);
num = asknum(prompt);
return num;
}
// checking 1 program module
int
getmax(const char *what,const int *arr,int count)
{
int idx;
int val;
int max = -1;
if (count > 0) {
max = arr[0];
for (idx = 1; idx < count; ++idx) {
val = arr[idx];
if (val > max)
max = val;
}
printf("highest %s value : %d\n", what, max);
}
return max;
}
int
main(void)
{
int idx;
int count = asknum("Enter the number of array elements");
int arr[count];
for (idx = 0; idx < count; ++idx)
arr[idx] = askval(idx);
getmax("value",arr,count);
int number[count];
for (idx = 0; idx < count; ++idx)
number[idx] = arr[idx];
//checking 2 main programs
getmax("score",number,count);
return 0;
}
just a newbie in C here. I'm trying to print out the factorial given the input using recursion & pointers, but when my input is 5, the output was 2293620. Can somebody help me with this? I'm not sure where did that number come from, because factorial of 5 should give me 120. Thanks for your help!
#include<stdio.h>
int countlength(int *num) {
int x = 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
Remove references from your code.
#include<stdio.h>
int countlength(int num) {
int x = 1;
if (num == 1) {
return 1;
} else {
return num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
In this case, I think you don't need to use pass by reference. You should use pass by value. Then your code will work perfectlyly
Change this line:
return num * countlength(num - 1);
to this:
int val = *num - 1;
return *num * countlength(&val);
So you correctly assign the value and pass it as a pointer.
int countlength(int *num) {
int x = *num - 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(&x);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}```
**Try this**
int countlength(int *num) {
if (*num == 1) {
return 1;
} else {
return *num * countlength(&(*num - 1));
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
try this
/int arr[n]; int n;/
void Linear_Search() {
int i, q, flag = 0, num, n;
int arr[n];
printf("Enter the number of array\n");
scanf("%d", n);
printf("Enter the numbers from which searched\n");
for (i = 0; i <= n; i++); {
scanf("%d", & arr[i]);
}
printf("enter the number to be searched\n");
scanf("%d", & q);
for (i = 0; i <= n; i++) {
if (q == arr[i]); {
num = q;
flag = 1;
}
}
if (flag == 1) printf("found! number is %d", num);
else printf("number not present in group\n");
getch();
}
void main() {
printf("ALL SEARCHING TECHNIQUE\n");
printf("Choices\n");
printf("1.Linear Search\n2.Binary Search\n3.Interpolation Search\n4.Jump Search\n");
void Linear_Search();
int select, l = 1;
scanf("%d", & select);
switch (select) {
case 1:
printf("This is Linear Search\n");
void Linear_Search();
break;
}
/case 2: printf("This is Binary Search\n"); void Binary_Search(); break; case 3: printf("This is Interpolation Search\n"); void Interpolation_Search(); break; case 4: printf("This is Jump Search\n"); void Jump_Search(); break; } printf("To continue PRESS 1 or PRESS ANY KEY"); scanf("%d",&l);/
getch();
}
You are including the return type of the function at the places where you are trying to call them. This turns them from a function call to a function prototype declaration. Remove the return type (void in these cases) at the places where the function shall be called.
This question already has answers here:
Variable leading zeroes in C99 printf
(1 answer)
printf string, variable length item
(2 answers)
Closed 5 years ago.
I've written a programm for college where I print out all prime number twins between two numbers. (f.e. between 1 and 12000)
In my printing statement i've written %04d for 4 digits. But what i want to do is to make this variable. (I tried %0%dd, but this didnt work.) I dont want to do write just the max digit count of int. I count hte digits of the int with int count = floor(log10(abs(b))) + 1;
Heres my complete code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPrime(int a);
void listOfPrimeNumberTwins(int a, int b);
typedef int twins[10000][2];
int main(){
int a;
int b;
printf("Check if prime:\nEnter number: ");
scanf(" %d", &a);
b = isPrime(a);
if (b == 0){
printf("No Prime!");
} else if (b == -1){
printf("Negative number!");
} else {
printf("Prime!");
}
printf("\nPrime Number Twins:\nEnter number 1: ");
scanf(" %d", &a);
printf("Enter number 2: ");
scanf(" %d", &b);
listOfPrimeNumberTwins(a,b);
}
int isPrime(int a){
int i;
int b = 0;
if (a == 1){
return 0;
}
if (a <= 0){
return -1;
}
for (i = 2; i < a; i++){
if (a % i == 0){
return 0;
}
}
return 1;
}
void listOfPrimeNumberTwins(int a, int b){
int count = floor(log10(abs(b))) + 1;
int i;
int j = 0;
twins c;
b -= 1;
for (i = a; i < b; i++){
if (i > 1 && isPrime(i) == 1 && isPrime(i + 2) == 1){
c[j][0] = i;
c[j][1] = i + 2;
j += 1;
}
}
if (j == 0){
printf("No Prime number twins between %d and %d!", a,b);
} else {
printf("Prime number twins between %d and %d:\n", a,b);
for (i = 0;i < j; i++){
printf("%04d\t<-->\t%04d\n", c[i][0],c[i][1]);
}
}
}
How can I achieve what I want? Or is it just impossible like I want it?