Floating Point Exception AND too large constant warning? - c

I am hesitant to post my code because this is for a school assignment, but basically I need to iterate through a uint64_t that can be very large.
So, I have a value that is basically going to be maybe 10 characters long. It originally was an int, and the code partially works (but doesn't finish creating the output it's supposed to) but then I'd get the error "warning: integer constant is so large that it is unsigned".
I'd then change that int to uint64_t (it's being multiplied by 10), and it would give me a floating point exception.
Any insight without the code?
EDIT: Adding little bits of code...
uint64_t number = 1U;
...
for (int x = 0; x < y; x++) {
number *= 10;
}
^ this gives me the floating point exception.
Not sure if this is enough to be helpful.

By default, integer literals are signed. If you use the U suffix on the literal, that tells the compiler to make it unsigned, e.g. 123456789U. That should clear up the warning about the constant being too large. Alternatively, you could use the UINT64_C() macro from <stdint.h>.
It's hard to know what's causing your FPE without seeing some code.

Have you checked how many times of the for loop executed? In your for loop, the number increased with a exponential ratio.I wrote a small program to show the situation.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4
5 #define MAXLOOP 100
6
7 int
8 main(void)
9 {
10 uint64_t number = 1U;
11 int i;
12
13 for(i = 0; i < MAXLOOP; ++i){
14 number *= 10;
15 printf("%d\t%lu\n", i, number);
16 }
17
18 return 0;
19 }
20
The output is:
0 10
1 100
2 1000
3 10000
4 100000
5 1000000
6 10000000
7 100000000
8 1000000000
9 10000000000
10 100000000000
11 1000000000000
12 10000000000000
13 100000000000000
14 1000000000000000
15 10000000000000000
16 100000000000000000
17 1000000000000000000
18 10000000000000000000
19 7766279631452241920 <--- overflow, uint64_t can store 18,446,744,073,709,551,616 as its max value
20 3875820019684212736
21 1864712049423024128
22 200376420520689664
23 2003764205206896640
24 1590897978359414784
25 15908979783594147840
26 11515845246265065472
27 4477988020393345024
28 7886392056514347008
29 5076944270305263616
30 13875954555633532928
31 9632337040368467968
32 4089650035136921600
33 4003012203950112768
34 3136633892082024448
35 12919594847110692864
36 68739955140067328
37 687399551400673280
38 6873995514006732800
39 13399722918938673152
40 4870020673419870208
41 11806718586779598848
42 7386721425538678784
43 80237960548581376
44 802379605485813760
45 8023796054858137600
46 6450984253743169536
47 9169610316303040512
48 17909126868192198656
49 13070572018536022016
50 1578511669393358848
51 15785116693933588480
52 10277214349659471872
53 10538423128046960640
54 13150510911921848320
55 2377900603251621888
56 5332261958806667264
57 16429131440647569408
58 16717361816799281152
59 1152921504606846976
60 11529215046068469760
61 4611686018427387904
62 9223372036854775808
63 0
64 0
65 0
66 0
67 0
68 0
69 0
70 0
71 0
72 0
73 0
74 0
75 0
76 0
77 0
78 0
79 0
80 0
81 0
82 0
83 0
84 0
85 0
86 0
87 0
88 0
89 0
90 0
91 0
92 0
93 0
94 0
95 0
96 0
97 0
98 0
99 0
I think it is easy for you to understand.:->

Related

2D matrix to 3D matrix with row to [row, col] mapping

I have a 2D matrix with in the 1st dimension different channels, and in the 2nd dimension time samples. I want to rearrange this to a 3D matrix, with in the 1st and 2nd dimension channels, and in the 3rd time samples.
The channels have to mapped according to a certain mapping. Right now I am using a for-loop to do so, but what would be a no-loop solution?
N_samples = 1000;
N_channels = 64;
channel_mapping = reshape(1:64, [8 8]).';
% Results in mapping: (can also be random)
% 1 2 3 4 5 6 7 8
% 9 10 11 12 13 14 15 16
% 17 18 19 20 21 22 23 24
% 25 26 27 28 29 30 31 32
% 33 34 35 36 37 38 39 40
% 41 42 43 44 45 46 47 48
% 49 50 51 52 53 55 55 56
% 57 58 59 60 61 62 63 64
data = rand(N_channels, N_samples);
data_grid = NaN(8,8, N_samples);
for k = 1:N_samples
tmp = data(:, k);
data_grid(:, :, k) = tmp(channel_mapping);
end
You can do it in one go as follows:
data_grid = reshape(data(channel_mapping, :), 8, 8, []);

SockMerchant Challenge Ruby Array#count not counting?

So, i'm doing a beginners challenge on HackerHank and, a strange behavior of ruby is boggling my mind.
The challenge is: find and count how many pairs there are in the array. (sock pairs)
Here's my code.
n = 100
ar = %w(50 49 38 49 78 36 25 96 10 67 78 58 98 8 53 1 4 7 29 6 59 93 74 3 67 47 12 85 84 40 81 85 89 70 33 66 6 9 13 67 75 42 24 73 49 28 25 5 86 53 10 44 45 35 47 11 81 10 47 16 49 79 52 89 100 36 6 57 96 18 23 71 11 99 95 12 78 19 16 64 23 77 7 19 11 5 81 43 14 27 11 63 57 62 3 56 50 9 13 45)
def sockMerchant(n, ar)
counter = 0
ar.each do |item|
if ar.count(item) >= 2
counter += ar.count(item)/2
ar.delete(item)
end
end
counter
end
print sockMerchant(n, ar)
The problem is, it doesn't count well. after running the function, in it's internal array ar still have countable pairs, and i prove it by running it again.
There's more. If you sort the array, it behaves differently.
it doesnt make sense to me.
you can check the behavior on this link
https://repl.it/repls/HuskyFrighteningNaturallanguage
You're deleting items from a collection while iterating over it - expect bad stuff to happen. In short, don't do that if you don't want to have such problems, see:
> arr = [1,2,1]
# => [1, 2, 1]
> arr.each {|x| puts x; arr.delete(x) }
# 1
# => [2]
We never get the 2 in our iteration.
A simple solution, that is a small variation of your code, could look as follows:
def sock_merchant(ar)
ar.uniq.sum do |item|
ar.count(item) / 2
end
end
Which is basically finding all unique socks, and then counting pairs for each of them.
Note that its complexity is n^2 as for each unique element n of the array, you have to go through the whole array in order to find all elements that are equal to n.
An alternative, first group all socks, then check how many pairs of each type we have:
ar.group_by(&:itself).sum { |k,v| v.size / 2 }
As ar.group_by(&:itself), short for ar.group_by { |x| x.itself } will loop through the array and create a hash looking like this:
{"50"=>["50", "50"], "49"=>["49", "49", "49", "49"], "38"=>["38"], ...}
And by calling sum on it, we'll iterate over it, summing the number of found elements (/2).

C programming to make multiplication table (1-12)

I want to make a program that takes a single integer command line argument (n) and makes n x n multiplication table resembling the 4x4 example below. It should be only 1 through 12, but shouldn't be no smaller or larger. So if there is not enough argument, it should be print a usuage: Usage: program <n> \n or if it is out of range then it should be print: n out of range (1-12)\n and exit.
Example:
./program 4
* 1 2 3 4
+----------------
1| 1 2 3 4
2| 2 4 6 8
3| 3 6 9 12
4| 4 8 12 16
My code I have tried but wasn't success. I have copied the compiled is not what I expected in the below.
#include <stdio.h>
int main(void)
{
int i, j;
printf(" +");
for (i = 1; i < 13; ++i)
{
printf("%#3d ", i);
printf("\n");
}
for (i = 1; i < 64; ++i)
{
printf("-");
printf("\n");
}
for (i = 1; i < 13; ++i)
{
printf("%#2d |", i);
for (j = 1; j < 11; ++j)
{
printf("%#3d ", i * j);
printf("\n");
}
}
return 0;
}
After compile:
gcc version 4.6.3
+ 1
2
3
4
5
6
7
8
9
10
11
12
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1 | 1
2
3
4
5
6
7
8
9
10
2 | 2
4
6
8
10
12
14
16
18
20
3 | 3
6
9
12
15
18
21
24
27
30
4 | 4
8
12
16
20
24
28
32
36
40
5 | 5
10
15
20
25
30
35
40
45
50
6 | 6
12
18
24
30
36
42
48
54
60
7 | 7
14
21
28
35
42
49
56
63
70
8 | 8
16
24
32
40
48
56
64
72
80
9 | 9
18
27
36
45
54
63
72
81
90
10 | 10
20
30
40
50
60
70
80
90
100
11 | 11
22
33
44
55
66
77
88
99
110
12 | 12
24
36
48
60
72
84
96
108
120
I have been stuck on this for 2 days and trying to figure it out why is it going straight down.. Thank you.
the following code:
corrects the output formatting,
checks if there is a command line parameter,
follows the axiom:
only one statement per line and (at most) one variable declaration per statement.
checks the command line parameter to assure it is numeric and in the range 1...12
limits the scope of each variable
calls puts() where appropriate as printf() is very expensive in CPU cycles
uses appropriate horizontal spacing for readability
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <ctype.h> // isdigit()
#define MAX_VALUE 12
#define MIN_VALUE 1
int main( int argc, char *argv[] )
{
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s <maxTableSize: range 1...12>\n", argv[0] );
exit( EXIT_FAILURE );
}
// implied else, user entered a command line parameter
if( !isdigit( argv[1][0] ) )
{
fprintf( stderr, "command line parameter not numeric\n" );
exit( EXIT_FAILURE );
}
// implied else, command line parameter starts with digit
int maxTableSize = atoi( argv[1] );
if( MIN_VALUE > maxTableSize || maxTableSize > MAX_VALUE )
{
fprintf( stderr, "command line parameter not in valid range ( 1...12 )\n");
exit( EXIT_FAILURE );
}
// implied else, command line parameter contains valid value
printf(" + ");
for ( int i = 1; i <= maxTableSize; ++i )
{
printf("%4d ", i);
}
puts("");
for ( int i = 1; i < 5+(maxTableSize*5); ++i )
{
printf( "-" );
}
puts("");
for ( int i = 1; i <= maxTableSize; ++i )
{
printf( "%2d |", i );
for ( int j = 1; j <= maxTableSize; ++j )
{
printf( "%4d ", i * j );
}
puts("");
}
return 0;
}
the following demonstrates the various conditions of running the program. (where untitled is the name of the program.)
./untitled
USAGE: ./untitled <maxTableSize: range 1...12>
./untitled a
command line parameter not numeric
./untitled 0
command line parameter not in valid range ( 1...12 )
./untitled 13
command line parameter not in valid range ( 1...12 )
./untitled 1
+ 1
--------
1 | 1
./untitled 2
+ 1 2
------------
1 | 1 2
2 | 2 4
.....
./untitled 12
+ 1 2 3 4 5 6 7 8 9 10 11 12
----------------------------------------------------------------
1 | 1 2 3 4 5 6 7 8 9 10 11 12
2 | 2 4 6 8 10 12 14 16 18 20 22 24
3 | 3 6 9 12 15 18 21 24 27 30 33 36
4 | 4 8 12 16 20 24 28 32 36 40 44 48
5 | 5 10 15 20 25 30 35 40 45 50 55 60
6 | 6 12 18 24 30 36 42 48 54 60 66 72
7 | 7 14 21 28 35 42 49 56 63 70 77 84
8 | 8 16 24 32 40 48 56 64 72 80 88 96
9 | 9 18 27 36 45 54 63 72 81 90 99 108
10 | 10 20 30 40 50 60 70 80 90 100 110 120
11 | 11 22 33 44 55 66 77 88 99 110 121 132
12 | 12 24 36 48 60 72 84 96 108 120 132 144
In your code, you are making 2 mistakes.
You are adding the printf ("\n") inside the for loop. This should be outside the for loop in case of single loop. In case of nested loop, the new line should be after each internal for loop.
You are using %#3d in printf. The # should be removed.
Other than that, after making few changes in printf alignment, the updated code is below
#include <stdio.h>
int main(void)
{
int i, j;
printf(" +");
for (i = 1; i < 13; ++i)
{
printf("%4d ", i);
}
printf("\n");
for (i = 1; i < 64; ++i)
{
printf("-");
}
printf("\n");
for (i = 1; i < 13; ++i)
{
printf("%2d |", i);
for (j = 1; j < 13; ++j)
{
printf("%4d ", i * j);
}
printf("\n");
}
return 0;
}
Output is as below
+ 1 2 3 4 5 6 7 8 9 10 11 12
---------------------------------------------------------------
1 | 1 2 3 4 5 6 7 8 9 10 11 12
2 | 2 4 6 8 10 12 14 16 18 20 22 24
3 | 3 6 9 12 15 18 21 24 27 30 33 36
4 | 4 8 12 16 20 24 28 32 36 40 44 48
5 | 5 10 15 20 25 30 35 40 45 50 55 60
6 | 6 12 18 24 30 36 42 48 54 60 66 72
7 | 7 14 21 28 35 42 49 56 63 70 77 84
8 | 8 16 24 32 40 48 56 64 72 80 88 96
9 | 9 18 27 36 45 54 63 72 81 90 99 108
10 | 10 20 30 40 50 60 70 80 90 100 110 120
11 | 11 22 33 44 55 66 77 88 99 110 121 132
12 | 12 24 36 48 60 72 84 96 108 120 132 144

Weird output when printing in C

Just started C after first year of java and am already confused. I try to print an array into a multiplication table. It works otherwise but I get zeroes after the first round of printing the array. I have allocated data in the whole array and it loops the right amount after checking. Still after the first print line I get 0 0 0 0 0 0 0 after the correct output.
#include<stdio.h>
int main(void)
{
int kertotaulu[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int kertoja;
int indeksi;
for(kertoja = 0; kertoja <=15; kertoja++) {
if(kertoja == 0) {
printf("x");
printf(" ");
for(indeksi = 0; indeksi< (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
}
for(indeksi = 0; indeksi< (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]*kertoja);
printf(" ");
}
printf("\n");
}
return 0;
}
and the output is:
x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
kertoja is zero the first time through the loop so your second indeksi loop multiplies each element by zero giving you all of the extra 0's. You could add the second indeksi loop into an else case to avoid this, I'm not sure exactly what output you're looking for however so...
The issue could be fixed by ommitting the second loop for the index 0 as follows.
int main(void)
{
int kertotaulu[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int kertoja;
int indeksi;
for (kertoja = 0; kertoja <= 15; kertoja++)
{
if (kertoja == 0)
{
printf("x");
printf(" ");
for (indeksi = 0; indeksi < (sizeof(kertotaulu)
/ sizeof(kertotaulu[0])); indeksi++)
{
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
}
else // <-- change
{
for (indeksi = 0; indeksi < (sizeof(kertotaulu)
/ sizeof(kertotaulu[0])); indeksi++)
{
printf("%d", kertotaulu[indeksi] * kertoja);
printf(" ");
}
}
printf("\n");
}
return 0;
}
For the case when kertoja is zero it does it's own loop, but it also continues on to the second loop. A simple new line and continue should fix it.
#include<stdio.h>
int main(void)
{
int kertotaulu[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int kertoja;
int indeksi;
for (kertoja = 0; kertoja <= 15; kertoja++) {
if (kertoja == 0) {
printf("x");
printf(" ");
for (indeksi = 0; indeksi < (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
printf("\n"); // New Line
continue; // Continue, goes to the top of loop again
}
for (indeksi = 0; indeksi < (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi] * kertoja);
printf(" ");
}
printf("\n");
}
return 0;
}

Can't get negative inputs set to zero. How can negative arrays get set?

The following code works for all positive inputs correctly but, negative values are bogus. It would work if negative values are set to 0 initially, because they do add. Example at the end.
#include <stdio.h> /* Necessary header */
#define MAX_RESPONDENTS 6
#define MIN_RESPONSE_VALUE -100 /* Abbreviated MiRV */
#define MAX_RESPONSE_VALUE 86 /* Abbreviated MaRV */
#define RESPONSE_VALUE 187 /* Equals |(MiRV)| + or - |(MaRV)| + 1 */
#define STOP 3
#define BREAK 1
int main(void)
{
CountRating();
return 0;
}
CountRating()
{
int ratingCounters, rating[RESPONSE_VALUE] = {0}, Counter, response;
for (ratingCounters = 0; ratingCounters < MAX_RESPONDENTS;)
{
int response = ratingCounters, stop = STOP;
printf("Enter a integer rating between %d and %d for the product: ", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE);
scanf("%d", &response);
if (response <= MAX_RESPONSE_VALUE && response >= MIN_RESPONSE_VALUE)
stop = STOP;
else
{
int stopElse = stop;
if (stopElse < BREAK)
break;
else
{
do
{
printf("\nNot within range. Try again.\nYou have %d more attempts before program outputs total:", stop);
scanf("%d", &response);
printf("\n");
--stop;
if (stop < BREAK)
break;
} while (response > MAX_RESPONSE_VALUE || response < MIN_RESPONSE_VALUE);
} if (stop < BREAK)
break;
}
++rating[response];
++ratingCounters;
}
printf("\nRating Number of Responses\n");
printf("------ -------------------");
for (Counter = MAX_RESPONSE_VALUE; Counter >= MIN_RESPONSE_VALUE; --Counter)
{
printf("\n%3d%24d", Counter, rating[Counter]);
}
}
Example Output:
Enter a integer rating between -100 and 86 for the product: 66
Enter a integer rating between -100 and 86 for the product: 66
Enter a integer rating between -100 and 86 for the product: 66
Enter a integer rating between -100 and 86 for the product: 66
Enter a integer rating between -100 and 86 for the product: 66
Enter a integer rating between -100 and 86 for the product: 55
Rating Number of Responses
------ -------------------
86 0
85 0
84 0
83 0
82 0
81 0
80 0
79 0
78 0
77 0
76 0
75 0
74 0
73 0
72 0
71 0
70 0
69 0
68 0
67 0
66 5
65 0
64 0
63 0
62 0
61 0
60 0
59 0
58 0
57 0
56 0
55 1
54 0
53 0
52 0
51 0
50 0
49 0
48 0
47 0
46 0
45 0
44 0
43 0
42 0
41 0
40 0
39 0
38 0
37 0
36 0
35 0
34 0
33 0
32 0
31 0
30 0
29 0
28 0
27 0
26 0
25 0
24 0
23 0
22 0
21 0
20 0
19 0
18 0
17 0
16 0
15 0
14 0
13 0
12 0
11 0
10 0
9 0
8 0
7 0
6 0
5 0
4 0
3 0
2 0
1 0
0 0
-1 55
-2 2686244
-3 2686244
-4 -3
-5 4206858
-6 4199517
-7 1965606432
-8 -2
-9 -533773290
-10 1965657301
-11 2686916
-12 1965975817
-13 2685900
-14 28
-15 2685972
-16 0
-17 2686720
-18 -1792050746
-19 1965606432
-20 1966156064
-21 1
-22 1965606483
-23 2685944
-24 17
-25 1965552124
-26 2685880
-27 1966146752
-28 1965532226
-29 2685868
-30 1966156064
-31 1
-32 0
-33 1966156064
-34 0
-35 28
-36 6105760
-37 1
-38 1965551966
-39 1965572357
-40 -2
-41 -533740746
-42 1965657301
-43 2685928
-44 1966156064
-45 2685768
-46 28
-47 1965572357
-48 0
-49 1966156064
-50 28
-51 -1792050874
-52 1965572357
-53 1
-54 1965572401
-55 2685816
-56 6097440
-57 1965552169
-58 2685752
-59 36
-60 1966146816
-61 0
-62 1966146816
-63 36
-64 0
-65 -1792050942
-66 1965552271
-67 1965552263
-68 2685756
-69 6105754
-70 4104
-71 4096
-72 6097428
-73 36
-74 -2
-75 358724962
-76 1999401429
-77 2685928
-78 -1
-79 2685472
-80 16777216
-81 6094848
-82 1
-83 0
-84 6105755
-85 6105760
-86 6095044
-87 8
-88 2
-89 1999167428
-90 1
-91 6095044
-92 -218103565
-93 2685640
-94 -254549010
-95 0
-96 1136989510
-97 -503245737
-98 2752512
-99 6098688
-100 0
Process returned 0 (0x0) execution time : 9.734 s
Press any key to continue.
Rating is an array of size 187. You're indexing negative values, which means C is accessing memory before the beginning of the array and printing whatever garbage values are stored there. You can only access values between 0 and 186 legally, so you should just add 100 to the index whenever you access and store rating. That way, the number -100 corresponds to index 0, -99 to index 1, etc.
The index of an array always starts from zero. You should not pass a negative index for an array. If you want to have negative indexes, add a constant to the index.
In your example code, you can add -MIN_RESPONSE_VALUE to the index, so accessing rating[k - MIN_RESPONSE] if you want to access index k.
Your code is assigning to negative indices of an array. This is generally not good - you have only reserved memory from 0:RESPONSE_VALUE-1, writing to anything outside that range will have unpredictable consequences. You have two general options. One is to map your range MIN_RESPONSE_VALUE:MAX_RESPONSE_VALUE to 0:(MAX_RESPONSE_VALUE-MIN_RESPONSE_VALUE). I.e. subtract MIN_RESPONSE_VALUE whenever you index rating:
++rating[response-MIN_RESPONSE_VALUE];
The other is to repoint response, giving it a new zero point. This will save you from having to subtract MIN_RESPONSE_VALUE all the time, but may be confusing to readers:
rating -= MIN_RESPONSE_VALUE;
Do this before the for loop. After doing this, rating[MIN_RESPONSE_VALUE] is a valid index, even though it is negative.
Actually the negative index is totally acceptable. The problem is that you index goes into areas outside memory dedicated for array on the stack. Changing rating[-1] you changing your stack outside the array and thus corrupting it. No wonder the values there are not initialized to zero with rating[RESPONSE_VALUE] = {0}, again they are outside the array and show you some data on your stack.
Took me a while but I got it. Thanks.
#include <stdio.h> /* Necessary header */
#define MAX_RESPONDENTS 6
#define MIN_RESPONSE_VALUE -100 /* Abbreviated MiRV */
#define MAX_RESPONSE_VALUE 86 /* Abbreviated MaRV */
#define RESPONSE_VALUE 187 /* Equals |(MiRV)| + or - |(MaRV)| + 1 */
#define STOP 3
#define BREAK 1
#define DOUBLE_RESPONSE_VALUE 374
int main(void)
{
CountRating();
return 0;
}
CountRating()
{
int ratingCounters, rating[DOUBLE_RESPONSE_VALUE] = {0}, Counter, response;
for (ratingCounters = 0; ratingCounters < MAX_RESPONDENTS;)
{
int response = ratingCounters, stop = STOP;
printf("Enter a integer rating between %d and %d for the product: ", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE);
scanf("%d", &response);
if (response <= MAX_RESPONSE_VALUE && response >= MIN_RESPONSE_VALUE)
stop = STOP;
else
{
int stopElse = stop;
if (stopElse < BREAK)
break;
else
{
do
{
printf("\nNot within range. Try again.\nYou have %d more attempts before program outputs total:", stop);
scanf("%d", &response);
printf("\n");
--stop;
if (stop < BREAK)
break;
} while (response > MAX_RESPONSE_VALUE || response < MIN_RESPONSE_VALUE);
} if (stop < BREAK)
break;
}
if (response < 0)
++rating[response + abs(RESPONSE_VALUE)];
else
++rating[response + abs(RESPONSE_VALUE)];
++ratingCounters;
}
printf("\nRating Number of Responses\n");
printf("------ -------------------");
for (Counter = MAX_RESPONSE_VALUE; Counter >= MIN_RESPONSE_VALUE; --Counter)
{
printf("\n%3d%24d", Counter, rating[Counter + (RESPONSE_VALUE)]);
}
}

Resources