Part 1 says to write a program that shows you how much a float “consumes”.
Here is what I came up with:
#include <stdio.h>
float *ptr;
int main(int argc, const char * argv[])
{
int i = 17;
int *addressOfI = &i;
printf("i stores its value at %p\n", addressOfI);
*addressOfI = 89;
printf("Now i is %d\n", i);
printf("An int is %zu bytes\n", sizeof(i));
printf("A pointer is %zu bytes\n", sizeof(addressOfI));
printf(“A float is %zu bytes\n”, sizeof(float)); //This tells me the the float is 4 bytes. Is this correct? Is this the same as consuming?
return 0;
}
Part 2 ask about the limits for the short :
I had to look up on google but here what I came across, the smallest is -32767 while the largest is 32767. Then, the largest # an unsigned short can store would be 65535.
signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement platforms)
unsigned char: 0 to 255
"plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
signed short: -32767 to 32767
unsigned short: 0 to 65535
signed int: -32767 to 32767
unsigned int: 0 to 65535
signed long: -2147483647 to 2147483647
unsigned long: 0 to 4294967295
signed long long: -9223372036854775807 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615
I would just like to confirm these are correct since there weren’t any post on these challenges.
int main(int argc, const char * argv[])
{
int ranger = 0;
int value = 1;
for (int place = 1; place < 16; place ++) {
ranger = ranger + value;
value =2;
}
printf(“The largest value a short can hold is %d\nThe smallest is %d\n”, ranger,-ranger);
printf(“The largest value an unsigned short can hold is %d\n”, ranger2 +1);
return 0;
}
Output
The largest value a short can hold is 32767
The smallest is -32767
The largest value an unsigned short can hold is 65535
Program ended with exit code: 0
#include <limits.h> // <- need to include this for SHRT_MIN, SHRT_MAX, USHRT_MAX
//First challenge
printf(“Size in bytes of a float is %zu\n”, sizeof(float);
//Second challenge
printf(“Minimum value of a signed short is %d\n”, SHRT_MIN);
printf(“Maximum value of a signed short is %d\n”, SHRT_MAX);
printf(“Maximum value of a unsigned short is %d\n”, USHRT_MAX);
int main(int argc, const char * argv[])
{
printf(“The range of an unsigned short is from 0 to %.0f\n”, unsignedMax(bytesToBits(sizeof(short))));
printf(“The range of a signed short is from %.0f to %.0f\n”, signedMin(bytesToBits(sizeof(short))), signedMax(bytesToBits(sizeof(short))));
}[/code]
The book doesn’t give us any SHRT_MIN, SHRT_MAX, USHRT_MAX yet. So i decided to make the challenge upon the information i got.
Here’s my solution:
[b]#include <stdio.h>
int main(int argc, const char * argv[])
{
short x = 0;
while (x > -1) {
x++;
}
x–;
printf(“The largest value of short is %d.\n”, x);
x = 0;
while (x < 1) {
x–;
}
x++;
printf(“The smallest value of short is %d.\n”, x);
unsigned short y = 1;
while (y > 0) {
y++;
}
y–;
printf(“The largest value of unsigned short is %d.\n”, y);
return 0;
}[/b]
And that’s what we got:
The largest value of short is 32767.
The smallest value of short is -32768.
The largest value of unsigned short is 65535.
Program ended with exit code: 0
int main(int argc, const char * argv[])
{
// Declare a variable X of type short and assign a value 0
short x = 0;
// Create a while loop, conditional statement: 'while x is greater than -1’
while (x > -1) {
// Increment x by 1
x++;
// Print the value of x after each increment
printf("%d\n", x);
/* It's a long output but displays what happens when the number is reached at its maximum value, 32768 - it starts back from minimum value, -32768. */
}
return 0;
[quote=“SaidNunaev”]The book doesn’t give us any SHRT_MIN, SHRT_MAX, USHRT_MAX yet. So i decided to make the challenge upon the information i got.
Here’s my solution:
[b]#include <stdio.h>
int main(int argc, const char * argv[])
{
short x = 0;
while (x > -1) {
x++;
}
x–;
printf(“The largest value of short is %d.\n”, x);
x = 0;
while (x < 1) {
x–;
}
x++;
printf(“The smallest value of short is %d.\n”, x);
unsigned short y = 1;
while (y > 0) {
y++;
}
y–;
printf(“The largest value of unsigned short is %d.\n”, y);
return 0;
}[/b]
And that’s what we got:
The largest value of short is 32767.
The smallest value of short is -32768.
The largest value of unsigned short is 65535.
Program ended with exit code: 0[/quote]
SaidNun, why do you decrement your x variable after incrementing it? It looks like it worked, but I don’t understand why.
My solution wasn’t as elegant, but I was able to answer the question. Note: I did not code for the unsigned int.
[code]printf(“The memory held by float is %lu.\n”, (sizeof(float)) );
short int smallestNumber;
short int largestNumber;
for (smallestNumber = 0; smallestNumber <= 0; smallestNumber--)
{
printf("The smallest number a short int can store is %d.\n", smallestNumber);
}
for (largestNumber = 0; largestNumber >= 0; largestNumber++)
{
printf("The largest number a short int can store is %d.\n", largestNumber);
}
}[/code]
This code prints each number from 0 to either limit. How would I code it to only print the final numbers?
I don’t speak english well, but i’ll try to explain.
Because the while loop continuous while conditional x>-1 is true (I started from 0). When x riches the largest amount (32767) it still x>-1 and the loop increments x one more time. Then x becomes the lowest amount (it can’t be larger then 32767 so it goes around and start from the lowest again). The conditional x>-1 is now false, and the while loop ends.
So the final amount of x is now to be decremented to get it back to the largest amount.
int main(int argc, const char * argv[])
{
// how much memory float consumes
printf("a float is %zu bytes\n", sizeof(float));
// what is the smallest number a short can hold and its largest
short x , y;
for (x; x > -1; x++) {
continue;
}
for (y; y < 1; y--) {
continue;
}
printf("%d\n %d\n", x, y);
// same question but unsigned small instead
unsigned short i;
for (i; i < 1; i--) {
continue;
}
printf("%d\n", i);
return 0;
}
why no one likes for loops they give you less writing
Initialize all variables explicitly before using them.
Initialize all variables explicitly before using them.
Initialize all variables explicitly before using them.
[quote=“ibex10”]It is working by accident, not by design
Initialize all variables explicitly before using them.
Initialize all variables explicitly before using them.
Initialize all variables explicitly before using them.[/quote]
[quote=“ibex10”]It is working by accident, not by design
Initialize all variables explicitly before using them.
Initialize all variables explicitly before using them.
Initialize all variables explicitly before using them.[/quote]
sorry I was so confused yesterday now I understand what you mean!
Here is the perfect code
#include <stdio.h>
int main(int argc, const char * argv[])
{
// how much memory float consumes
printf("a float consumes %zu bytes\n\n", sizeof(float));
// what is the smallest number a short can hold and its largest
short x;
short y;
for (x = 0; x > -1; x++) {
continue;
}
for (y = 0; y < 1; y--) {
continue;
}
printf("\nSmallest short %d\nlargest short %d\n", x, y);
// same question but unsigned short instead
unsigned short i;
for (i = 0; i < 1; i--) {
continue;
}
printf("largest unsigned short is %d\n", i);
return 0;
}
So in other words, once the value hits 32768 the sign bit is set to 1, so the value becomes -0. 32769 would be -1, 32770 would be -2, etc. Keep counting until the value becomes high enough where the sign bit would revert to 0 again and it starts counting up positive numbers again.