I'll keep it simple.
Documentation really does NOT matter here. The intent seems pretty clear.
Sum all integers while using a VLA.
Header is fine. The most complicated thing happening here is I/O.
Return can be cast, it's personal preference.
You can use return 0;
or return (0);
or return(0);
and if you're really feeling saucy; as horrific as it may seem and startle the community into mass pandemic of terror and anxiety... you can include stdlib.h, void main(void), and use exit(0).
Even if short
is 2 bytes, you still have up to a range of -65,537 to 65,536
possible values. Assuming most systems can handle a 4 byte int
, long long
isn't really necessary. unsigned
, in this context, can cause serious issues in even the most basic of concepts such as this one. You should allow for signed values. I'll explain why in a bit. But for now, we'll change this
unsigned short int num = 0;
to this
int num = 0;
The while()
loop is good. If num is <= 0
or num is >= 256
prompt the user again. Your filtering the input, but there is a catch with scanf()
and its that it reads up until the first whitespace character. You need a way to clear the input.
while (getchar() != '\n');
or while (getchar() != '\n') continue;
usually solves this problem. That way, any invalid input is cleared and new input is ready. Also, you note that while num is less than zero
. If your num
variable is of type unsigned
, you've created a semantic error. One that took me awhile to get used to too. It's incredibly easy to make it as well. That's why it should be signed
and not unsigned
. unsigned
types have their place and this context is not it.
while(num < 0 || num > 255)
{
printf("Enter number of values to sum: ");
scanf("%d", &num);
while (getchar() != '\n') continue; //clear the input buffer
}
as for the vla arr, the total variable, and the i variable...
unsigned long long int arr[num];
unsigned long long int total = 0;
unsigned short int i;
should really be
int arr[num], total, i;
you don't even really need to declare i, but if you want compatibility, you can declare i outside of the for() loop. Again, it depends on context and preference.
again, we don't need a long long
type. just an int
type.
scanf("%lld", arr + i);
should really just be
scanf("%d", &arr[i]);
since we are working with arrays, not pointers (even though, an array is a type of pointer), the intent is clearer and consistency runs throughout your code; and remember, were storing the value into that array member.
we don't need to offset num
by 1 since i
needs to be less than num. The elements are already offset by one and initializing i to 0 accomplishes that goal. but really, we don't even really need that code at all. all we need to do is print the total.
for(i = 0; i < num; i++)
{
scanf("%d", &arr);
while (getchar() != '\n') continue; //clear the buffer
total += arr[i];
printf(" + ");
}
printf("total sum is %d\n", total);
but you should get the idea by now.
in all, this is my revised code. while, i'll admit, it is very buggy and unstable. its a basic framework to play with and displays my point of clearing whitespace produced by using scanf(). it accomplishes the task of creating a vla and the data types used fall well within the bounds created by the initial input.
assuming vlas are being used, we can assume that a c99 compliant compiler is being used.
#include <stdio.h>
int main(void)
{
int num = 0;
printf("Enter number of values to sum: ");
while(1 != scanf("%d", &num) && (num < 0 || num > 256))
{
printf("Enter number of values to sum: ");
while (getchar() != '\n'); //clear buffer
scanf("%d", &num);
}
while (getchar() != '\n');
int arr[num], total = 0;
printf("%d number(s) to be summed: ", num);
for(int i = 0, j = num - 1; i < num; i++, j--)
{
while (1 != scanf("%d", &arr[i]))
{
printf("%d number(s) to be summed: ", j);
while (getchar() != '\n') continue; //clear buffer
scanf("%d", &num);
}
while (getchar() != '\n');
total += arr[i];
if (j != 0) printf("%d number(s) to be summed: ", j);
}
printf("The total sum is %d\n", total);
return 0;
}