Weird output when printing in C - 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;
}

Related

My code for "Count only Repeated" is not working. Have written clean code(according to me) that is easy to understand

Here is the question : https://www.geeksforgeeks.org/count-of-only-repeated-element-in-a-sorted-array-of-consecutive-elements/
Here is my code :
public static Point findRepeating(Integer arr[],int n)
{
// Point(return type) is a class having first and second as data memebres where first is number
// repeating and second is number of times it is repeating
if(arr[0] == arr[n-1]) // whole array has only one number
return new Point(arr[0],n);
int low = 0,high = n-1,mid = (low+high)/2, repNumber = 0;
while(high-low > 1) // in this loop only I'm trying to find the number that is repeating
{
mid = (low+high)/2;
if(arr[mid] == arr[mid-1] || arr[mid] == arr[mid+1])
{
repNumber = arr[mid];
break;
}
if((arr[low]+(mid-low)) == arr[mid])
low = mid+1;
else
high = mid;
}
int startIndex=0,endIndex=0;
// now I'll find the start and end index of the repeating number
// doing this by finding number just smaller and just larger than repeating number
if(repNumber == arr[0])
{
startIndex = 0;
endIndex = Arrays.binarySearch(arr,repNumber+1);
return new Point(repNumber,endIndex);
}
if(repNumber == arr[n-1])
{
endIndex = n-1;
startIndex = Arrays.binarySearch(arr,repNumber-1);
return new Point(repNumber,endIndex-startIndex);
}
else
{
startIndex = Arrays.binarySearch(arr,repNumber-1) + 1;
endIndex = Arrays.binarySearch(arr,repNumber+1);
return new Point(repNumber,endIndex-startIndex);
}
}
The test case where my code fails is(whole input is not visbile as it's huge):
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
28566
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 15.................
Its Correct output is:
16932 10086
And Your Code's output is:
0 0
This question can be simply solved using Linear Search within Time Complexity less than O(N) ( O(N) in worst case ).
In first step we can check whether the first and last element of array are equal or not?
If Yes,
We can return the size of array as our answer.
Otherwise we can check Linearly using two methods
Method 1 : We can take the consecutive Differences between two Numbers and check whether they give us Zero as result or not
int count = 0;
vector<int>arr = { 1,2,2,2,2,2,2,3 };
for(int i = 0; i < arr.size() ; i++){
if(arr[i+1] - arr[i] == 0 ){
count++;
}
}
Method 2 : By Taking XOR of two consecutive elements of array and When XOR is zero increment the count
int count = 0;
vector<int>arr = { 1,2,2,2,2,2,2,3 };
for(int i = 0; i < arr.size() ; i++){
if((arr[i+1]^arr[i]) == 0 ){
count++;
}
}

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

Adding Character String with Numbers / Concatenate Number with String in Matlab Array

How to add character ',' or '+' in a matlab 2d array.
I've tried the following way.
clc
clear all
close all
min=0;
max=1052;
random_int = randi([min max],5,10)
% random_int=[515,586,942,742;353,588,916,436]
% load('Random_Int_x.mat')
% random_int
[m,n]=size(random_int);
for i=1:1:m
allOneString = sprintf('%d,' , random_int(i,:));
allOneString= allOneString(1:end-1)% strip final comma
Str_1(i,:)=allOneString
% allOneString= strjoin(arrayfun(#(x) num2str(x),random_int(i,:),'UniformOutput',false),',');
end
Str_1
Example of Input / Matrix
random_int =
2 9 7 7 9 8 2 5 7 5
6 1 9 9 6 1 9 4 1 0
5 0 8 8 5 6 9 0 4 6
0 9 9 8 7 5 6 3 7 8
8 4 2 0 5 5 1 8 2 6
Output:
Str_1 =
5×19 char array
'2,9,7,7,9,8,2,5,7,5'
'6,1,9,9,6,1,9,4,1,0'
'5,0,8,8,5,6,9,0,4,6'
'0,9,9,8,7,5,6,3,7,8'
'8,4,2,0,5,5,1,8,2,6'
This works properly with random number between 0-9.. However if I put input above 9 --> 10 .. then matlab throws matrix dimension error.
Subscripted assignment dimension mismatch.
Error in Number_with_String (line 14)
Str_1(i,:)=allOneString;
For Input above 9:
random_int =
76 96 88 23 26 25 92 5 61 86
87 69 32 36 86 39 46 21 55 69
42 26 56 69 55 97 91 78 76 41
74 74 24 3 46 52 29 70 88 4
7 48 13 69 15 12 79 91 90 24
Expecting output:
'76,96,88,23,26,25,92,5,61,86'
'87,69,32,36,86,39,46,21,55,69' ... etc
Any suggestion to resolve this ..
Here's a way:
random_int = randi([0 500],5,10); % example data
y = mat2cell(random_int, ones(1,size(random_int,1)), size(random_int,2)); % split into rows
y = cellfun(#(x) sprintf('%i,', x), y, 'UniformOutput', false); % strings with commas
y = cellfun(#(s) s(1:end-1), y, 'UniformOutput', false); % remove last comma from each
Example result:
>> y
y =
5×1 cell array
'74,281,294,376,124,203,211,170,242,334'
'488,268,31,84,404,74,205,178,215,20'
'120,242,390,37,113,199,140,375,395,469'
'455,94,115,476,28,20,365,213,181,31'
'130,62,138,421,261,105,114,226,398,90'
I would recommend you use string which shipped in 16b. You can convert the result to char or cellstr if you need.
>> min=0; max=1052;
>> random_int = randi([min max],5,10)
random_int =
532 145 857 264 616 793 558 494 327 688
736 157 256 648 578 400 820 12 556 725
938 271 978 498 965 597 983 354 174 787
1010 885 368 370 300 79 136 170 633 474
576 267 207 874 797 56 598 836 276 88
>> str = join(string(random_int),',')
str =
5×1 string array
"532,145,857,264,616,793,558,494,327,688"
"736,157,256,648,578,400,820,12,556,725"
"938,271,978,498,965,597,983,354,174,787"
"1010,885,368,370,300,79,136,170,633,474"
"576,267,207,874,797,56,598,836,276,88"
>> char(str)
ans =
5×39 char array
'532,145,857,264,616,793,558,494,327,688'
'736,157,256,648,578,400,820,12,556,725 '
'938,271,978,498,965,597,983,354,174,787'
'1010,885,368,370,300,79,136,170,633,474'
'576,267,207,874,797,56,598,836,276,88 '
clc
clear all
close all
min=0;
max=1052;
random_int = randi([min max],200,10);
[m,n]=size(random_int);
for i=1:1:m
allOneString = sprintf('%d,' , random_int(i,:));
allOneString= allOneString(1:end-1); % strip final comma
Str_1{i}=allOneString;
end
Str_1=Str_1'

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)]);
}
}

Floating Point Exception AND too large constant warning?

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.:->

Resources