I just finished my first homework assignment in a C class, and my homework file isn't running to completion.
Here is my compiler code:
$ cd hw1
$ gcc -o hw1 hw1.c
$ ./hw1
Here is the code from hw1.c
#include <stdio.h>
#include <limits.h>
void ranges() {
printf("Signed Char/n");
printf("Minimum Value = %d\n", SCHAR_MIN);
printf("Maximum Value = %d\n", SCHAR_MAX);
printf("Unsigned Char\n");
printf("Minimum Value = 0\n");
printf("Maximum Value = %d\n", UCHAR_MAX);
printf("Signed Short \n");
printf("Minimum value = %d\n", SHRT_MIN);
printf("Maximum value = %d\n", SHRT_MAX);
printf("Unsigned Short \n");
printf("Minimum Value = 0\n");
printf("Maximum value = %d\n", USHRT_MAX);
printf("Signed Integer \n");
printf("Minimum value = %d\n", INT_MIN);
printf("Maximum value = %d\n", INT_MAX);
printf("Unsigned Integer \n");
printf("Minimum Value = 0\n");
printf("Maximum value = %ud\n", UINT_MAX);
printf("Signed Long \n");
printf("Minimum value = %li\n", LONG_MIN);
printf("Maximum value = %li\n", LONG_MAX);
printf("Unsigned Long \n");
printf("Minimum Value = 0\n");
printf("Maximum value = %lu\n", ULONG_MAX);
}
int factorial1(int n) {
int res = 1;
int i;
for (i = 1; i <= n; i++)
res = res * i;
return res;
}
int factorial2(int n) {
int res = 1;
int i = 1;
while (i <= n);{
res = res * i;
i++;
}
return res;
}
int factorial3(int n) {
int res = 1;
int i = 1;
do{
res = res * i;
i++;
}
while (i <= n);
return res;
}
int factorial4(int n) {
if (n >= 1)
return n*factorial4(n-1);
else
return 1;
}
void types() {
char a1,b1,c1,*p1,*p2,*p3;
short a2,b2,c2,*p4,*p5,*p6;
int a3,b3,c3,*p7,*p8,*p9;
double a4,b4,c4,*p10,*p11,*p12;
p1=&a1;
p2=&b1;
p3=&c1;
p4=&a2;
p5=&b2;
p6=&c2;
p7=&a3;
p8=&b3;
p9=&c3;
p10=&a4;
p11=&b4;
p12=&c4;
printf("\nAddress of %p %u\t", p1,p1);
printf("\nAddress of %p %u\t", p2,p2);
printf("\nAddress of %p %u\t", p3,p3);
printf("\nAddress of %p %u\t", p4,p4);
printf("\nAddress of %p %u\t", p5,p5);
printf("\nAddress of %p %u\t", p6,p6);
printf("\nAddress of %p %u\t", p7,p7);
printf("\nAddress of %p %u\t", p8,p8);
printf("\nAddress of %p %u\t", p9,p9);
printf("\nAddress of %p %u\t", p10,p10);
printf("\nAddress of %p %u\t", p11,p11);
printf("\nAddress of %p %u\n\n\n\n", p12,p12);
}
// test code; do not modify
int main() {
ranges();
printf("factorial1(10) = %d\n", factorial1(10));
printf("factorial2(10) = %d\n", factorial2(10));
printf("factorial3(10) = %d\n", factorial3(10));
printf("factorial4(10) = %d\n", factorial4(10));
printf("\n");
types();
return 0;
}
However, when I run the program with $ ./hw1 the output stops after the factorial1 function. Again, I'm new to C so is this normal? Do I need to do something to see the rest of the output? Apologize for the newbie question and tried searching but couldn't figure out if my code is wrong or it's something else. Here's the output:
Signed Char
Minimum Value = -128
Maximum Value = 127
Unsigned Char
Minimum Value = 0
Maximum Value = 255
Signed Short
Minimum value = -32768
Maximum value = 32767
Unsigned Short
Minimum Value = 0
Maximum value = 65535
Signed Integer
Minimum value = -2147483648
Maximum value = 2147483647
Unsigned Integer
Minimum Value = 0
Maximum value = 4294967295d
Signed Long
Minimum value = -9223372036854775808
Maximum value = 9223372036854775807
Unsigned Long
Minimum Value = 0
Maximum value = 18446744073709551615
factorial1(10) = 3628800
Thanks
You have a typo in factorial2
causing an infinite loop:
while (i <= n);{
should be:
while (i <= n) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.