Error converting a integer value to float in c - c

What I have so far...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 23
#define COLS 78
int main()
{
srand(time(NULL));
int arr[ROWS][COLS];
int sum = 0, counter1 = 0;
int k;
int i;
for (i = 0; i < ROWS; i++)
{
for (k = 0; k < COLS; k++)
{
int j = rand() % 10;
arr[i][k] = j;
printf("%d", arr[i][k]);
sum += arr[i][k];
counter1++;
}
printf("\n");
}
printf("\n");
printf("Average = %f\n", float(sum / counter1)); //Not getting exact decimal
printf("Total numbers = %d\n", counter1);
printf("Sum of all numbers = %d\n", sum);
system("pause");
}
//Guessing the float is not converting as it should
Output so far....
422620351595919054460397694388416319310552973446498175458548095262118564755749
372438759420195802623062987655437952542263796363224469905714526364539742622586
124057899145417666187341286327350448786294141128615529044980269471365598313616
049149926286317172502475330649055931769733144700851693923585818341846097713250
826490802265735183843662244505193942554706854717207204487697516652123593599812
746605634674496934691593122520210519087830081107377133858638184877543092242563
172193699659454833537193985400677695430588428833192355923118027599256651906912
137220359907772463940456613321876957716810615732837946886780325557272201117024
970243922687865367166306388461501128523059854106020838236007732376011146109340
239699228705450117510917862089719947717228921040726908237444581851160698373585
977683407031265082736130690889715335368791599705700863736964292214408266990418
536416318005520152773513475211623892873091447652802755401011207564392549940648
363921632561931849591970346553864295588487844510186065223062209484132581237554
163065518327032754274153946192470922485557045255627009864269391575284952286729
910378772792028085274954077343530447604501744264499511471498074681140863439500
617676961625579079414287810415495010710519733811672377499905168756599387590803
885768234776892368128405809649999684460473809170548180074483476110165080161362
308905170626655925452893089531708914818032726022522429229062646282887593747734
210270837200571461718554091036728525587376768397741678405601722794577171279012
421649999013761899858408492974830996084297030747093760611147983343593657910519
461743953198670501367758536958306703445033922711724727706025545309033925365602
635580784707723652610577238350330553362131582484629421787969994692199573628406
521293251392156351047950677006976709658442231650692040031419198232429972214834
Average = 4.000000
Total numbers = 1794
Sum of all numbers = 8054
Press any key to continue . . .

You can format the number to 2 decimal place by placing ".2f" :
printf("Average = %.2f\n", float(sum) / float(counter1));

float(sum / counter1) --> (float)sum / counter1
โ€“ BLUEPIXY

Related

Why won't the average column amount not calculate?

I want to print out the average amount of an 2D array column, by filling the matrix with random numbers
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int m = 0;
int n = 0;
int array[m][n];
double ran_num = (double)rand() / RAND_MAX;
double avg_col[] = {0};
printf("Enter (m, n > 0): ");
scanf("%d, %d", &m, &n);
for(size_t i = 0; i <= m; ++i){
for(size_t j = 0; j <= n; ++j){
array[i][j] = ran_num;
avg_col[j] += array[i][j] / m;
}
}
for(int i = 0; i < n; i++){
printf("Average of column %d : %.3f\n", i ,avg_col[i]);
}
return 0;
}
But the output is:
Average of column 0 : 0.000
Average of column 1 : 0.000
Average of column 2 : 0.000
I can't figure out where the problem is.
Maybe you can help me, I would really appreciate it.
In this line:
avg_col[j] += array[i][j] / m;
variable array[i][j] is an integer, and variable m is an integer.
So you are doing integer division. If the denominator m is greater than the numerator array[i][j], then the result is ZERO.
Example:
5 / 10; // Humans think the result is 0.5. Programmers know the result is 0.
Fixed up for you.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int m = 0;
int n = 0;
printf("Enter (m, n > 0): ");
scanf("%d, %d", &m, &n);
double array[m][n];
double avg_col[n];
memset(avg_col, 0, sizeof(avg_col));
for(size_t i = 0; i < m; ++i){
for(size_t j = 0; j < n; ++j){
array[i][j] = ((double)rand()) / RAND_MAX;
avg_col[j] += (double)array[i][j] / m;
}
}
for(int i = 0; i < n; i++){
printf("Average of column %d : %.3f\n", i ,avg_col[i]);
}
return 0;
}
Output (with input 5, 10)
Success #stdin #stdout 0s 5460KB
Enter (m, n > 0): 5, 10
Average of column 0 : 0.475
Average of column 1 : 0.575
Average of column 2 : 0.460
Average of column 3 : 0.661
Average of column 4 : 0.588
Average of column 5 : 0.478
Average of column 6 : 0.480
Average of column 7 : 0.697
Average of column 8 : 0.356
Average of column 9 : 0.620

i wrote a code for sum of series but i am not getting any output

The question was to add first seven terms of the following series using for loop
1/1! + 2/2! + 3/3! ....
I thought i might be loosing decimal point due to int but i am not even getting wrong answer.
I ran the code in terminal but it just keeps running no output.
Thanks for helping out.
#include<stdio.h>
int main()
{
int n;
float a;
float v=0.0;
for(n=1;n<=7;n++)
{
a=1.0;
while(n>0)
{
a=a/n;
n--;
}
v=v+n*a;
}
printf(" sum is %f ",v);
return 0;
}
#include <stdio.h>
int fact(int n) {
int product = 1;
for (int i = 1; i < n+1; i++) {
product *= i;
}
return product;
}
int main() {
double sum = 0.0;
for(int i = 1; i < 7+1; i++) {
sum += double(i) / fact(i);
}
printf("Sum is %f\n", sum);
return 0;
}
The series is 1/1! + 2/2! + .. n terms can be written as 1 + 1/1! + 1/2! + 1/3! + ...(n-1) terms .
So it is 1 + sum of 1/i! where i=1 to i=n-1 terms.
So I have taken sum=1 as initial value.
#include <stdio.h>
//this is the function for calculating n!
int fact(int n) {
int product = 1;
for (int i = 1; i < n+1; i++) {
product *= i;
}
return product;
}
int main() {
int n=5;
//we are taking sum=1 as initial value
double sum = 1.0;
//now we run 1/1! + 1/2! + 1/3! + 1/4! i.e. upto (n-1) terms (here n=5 => so n-1 = 4)
for(int i = 1; i < n; i++)
{
sum += (1.0) / fact(i);
}
printf("Sum is %f\n", sum);
return 0;
}
As #DeBARtha mentioned in the original code i was incrementing and then decreasing n causing to loop to run repeatedly.
By using one more variable (m) for the while loop and then decreasing it while increasing the 'n' in the for loop the problem gets resolved.
#include<stdio.h>
int main()
{
int n,m;
float a;
float v=0.0;
for(n=1;n<=7;n++)
{
m=n;
a=1.0;
while(m>0)
{
a=a/m;
m--;
}
v=v+n*a;
}
printf(" sum is %f ",v);
return 0;
}

Converting array to 8-bit codes in c

I have an array of 200 elements. I want to divide this array into 25 parts and find the RMS values โ€‹โ€‹of these parts and print 0 if it is less than 20, 1 if it is greater.
Example
signal1[200]
Output
data[8]={0,1,0,0,1,0,1,1)
I find code for RMS calculation
#include <stdio.h>
#include <math.h>
double rms(double* v, int n)
{
int i;
double sum = 0.0;
for (i = 0; i < n; i++)
sum += v[i] * v[i];
return sqrt(sum / n);
}
int main(void)
{
double v[] = {3,-3,7,1,-3,9,8,1,3,2,-6,-4,-1,-6,7,-6,-6,-7,-6,-1,-4,9,-1,-7,9,48,-6,-39,-24,-9,10,-24,10,21,-28,-
39,-21,-18,-8,1,-42,-24,30,-48,43,23,-1,8,-27,-4,47,5,2,-27,-1,13,18,-11,-13,49,-47,39,42,30,-41,-24,-17,18,-
37,22,-40,16,-1,28,22,41,39,-17,20,-31,-47,25,0,-2,41,11,12,36,31,8,-32,-26,39,-48,-1,-34,48,21,0,-3,-9,4,-10,-
9,0,-8,7,7,5,-7,3,0,10,3,6,-1,-1,7,-9,-8,-7,-2,7,6,-9,-10,3,-8,16,13,-21,-7,-49,49,-34,-40,-13,-30,-1,-16,46,42,-
45,24,-23,-8,5,45,-8,49,-20,20,17,4,20,17,-33,-38,50,-33,-47,6,39,17,-31,-13,-4,49,-35,36,15,-12,-31,-7,-2,-
8,2,-6,-2,2,-5,-4,2,-5,7,10,5,-3,2,-8,9,8,7,-5,2,-10,-2,-4,-7,-7};
printf("%f\n", rms(v, sizeof(v) / sizeof(double)));
return 0;
}
can you help me please ?
Simply call the function rms with 25 elements at a time:
int main(void)
{
double v[] = {3,-3,7,1,-3,9,8,1,3,2,-6,-4,-1,-6,7,-6,-6,-7,-6,-1,-4,9,-1,-7,9,48,-6,-39,-24,-9,10,-24,10,21,-28,-
39,-21,-18,-8,1,-42,-24,30,-48,43,23,-1,8,-27,-4,47,5,2,-27,-1,13,18,-11,-13,49,-47,39,42,30,-41,-24,-17,18,-
37,22,-40,16,-1,28,22,41,39,-17,20,-31,-47,25,0,-2,41,11,12,36,31,8,-32,-26,39,-48,-1,-34,48,21,0,-3,-9,4,-10,-
9,0,-8,7,7,5,-7,3,0,10,3,6,-1,-1,7,-9,-8,-7,-2,7,6,-9,-10,3,-8,16,13,-21,-7,-49,49,-34,-40,-13,-30,-1,-16,46,42,-
45,24,-23,-8,5,45,-8,49,-20,20,17,4,20,17,-33,-38,50,-33,-47,6,39,17,-31,-13,-4,49,-35,36,15,-12,-31,-7,-2,-
8,2,-6,-2,2,-5,-4,2,-5,7,10,5,-3,2,-8,9,8,7,-5,2,-10,-2,-4,-7,-7};
int data[8], i;
for (i = 0; i < 8; i++) {
data[i] = rms(v + 25 * i, 25) > 20.0;
}
for (i = 0; i < 8; i++) {
printf(" %d", data[i]);
}
putchar('\n');
return 0;
}
The expression v + i gives you the address of the i:th element of v.

for loop unexpectedly jumping down in value

Goldbach's conjecture states that every even integer over 4 is the sum of two primes, I am writing a program in C to find these pairs. To do this it first finds all the primes less than a user given number. I have a for loop to iterate from 4 to the user given number and find the pairs within the loop body. When that loop gets to about around 40, suddenly jumps back down by about 30 and then continues to iterate up (with user input 50 it jumped from 38 to 9, with input 60 it jumped from 42 to 7). I can't figure out why this is happening. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
struct pair{
int a;
int b;
}pair_t;
int main(){
int N;
int numPrimes = 1;
int *primes = malloc(100*sizeof(int));
int isPrime = 1;
primes[0] = 2;
int timesRealloc = 0;
int availableSlots = 100;
printf("Please enter the largest even number you want to find the Goldbach pair for: \n");
scanf("%d", &N);
struct pair pairs[N/2 + 4];
int j = 0;
int i;
for (i = 3; i <= N; i+=2){
j = 0;
isPrime = 1;
while (primes[j] <= sqrt(i)) {
if (i%primes[j] == 0) {
isPrime = 0;
break;
}
j++;
}
if (isPrime == 1){
primes[numPrimes] = i;
numPrimes++;
}
if (availableSlots == numPrimes){
timesRealloc++;
availableSlots += 100;
primes = realloc(primes, availableSlots*sizeof(int));
}
}
printf("The largest prime I found was %d\n", primes[(numPrimes-1)]);
int k;
for (i=4; i<=N; i+=2){
printf("i is %d, N is %d\n", i, N);
if (i > N){ break; }
for (j=0; j<numPrimes; j++){
for (k=0; k<numPrimes; k++){
int sum = primes[j] + primes[k];
if(sum == i){
pairs[i].a = primes[j];
pairs[i].b = primes[k];
}
}
}
}
for (i=4; i<=N; i+=2){
printf("%d is the sum of %d and %d\n", i, pairs[i].a, pairs[i].b);
}
return 0;
}
You attempt to be space efficient by compressing the pairs array to just hold every other (even) number and start from 4 instead of zero. However, you miscalculate its size and then when you go to use it, you treat it like it hasn't been compressed and that there's a slot for every natural number.
The code suffers from having the prime array calculation in main() along with the other code, this is best separated out. And when it looks for pairs, it doesn't quit when it finds one, nor when it starts getting sums greater than the target. My rework below attempts to address all of these issues:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define INITIAL_SLOTS (100)
struct pair {
int a;
int b;
} pair_t;
int compute_primes(int limit, unsigned **primes, int size) {
int numPrimes = 0;
(*primes)[numPrimes++] = 2;
for (int i = 3; i <= limit; i += 2) {
bool isPrime = true;
for (int j = 0; (*primes)[j] <= i / (*primes)[j]; j++) {
if (i % (*primes)[j] == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
(*primes)[numPrimes++] = i;
}
if (numPrimes == size) {
size *= 2;
*primes = realloc(*primes, size * sizeof(unsigned));
}
}
return numPrimes;
}
int main() {
int N;
printf("Please enter the largest even number you want to find the Goldbach pair for: \n");
scanf("%d", &N);
unsigned *primes = calloc(INITIAL_SLOTS, sizeof(unsigned));
int numPrimes = compute_primes(N, &primes, INITIAL_SLOTS);
printf("The largest prime I found was %d\n", primes[numPrimes - 1]);
struct pair pairs[(N - 4) / 2 + 1]; // compressed data structure
for (int i = 4; i <= N; i += 2) {
int offset = (i - 4) / 2; // compressed index
bool found = false;
for (int j = 0; ! found && j < numPrimes; j++) {
for (int k = 0; ! found && k < numPrimes; k++) {
int sum = primes[j] + primes[k];
if (sum == i) {
pairs[offset].a = primes[j];
pairs[offset].b = primes[k];
found = true;
} else if (sum > i) {
break;
}
}
}
}
for (int i = 4; i <= N; i += 2) {
int offset = (i - 4) / 2; // compressed index
printf("%d is the sum of %d and %d\n", i, pairs[offset].a, pairs[offset].b);
}
free(primes);
return 0;
}
OUTPUT
> ./a.out
Please enter the largest even number you want to find the Goldbach pair for:
10000
The largest prime I found was 9973
4 is the sum of 2 and 2
6 is the sum of 3 and 3
8 is the sum of 3 and 5
10 is the sum of 3 and 7
12 is the sum of 5 and 7
14 is the sum of 3 and 11
...
9990 is the sum of 17 and 9973
9992 is the sum of 19 and 9973
9994 is the sum of 53 and 9941
9996 is the sum of 23 and 9973
9998 is the sum of 31 and 9967
10000 is the sum of 59 and 9941
>

Fill a defined 2D Array with random numbers in C / Average / Max Value

I've created this 2D 21x21 array that has all it's values set to -1. I wrote it to print the address and value and somehow it only starts at [6][19] why?
What i want to do is to replace some of the -1 values with random numbers from 0 to 100 in the same array. I know i need to seed it with srand but i'm having problems connecting the functions since i'm a total beginner in C.
EDIT 1:
Now i can print the whole array and fill it with random numbers. For the -1 values i just assigned directly which for this case its fine.
What i'm trying now is finding the average of all the values and the maximum number, so what i have is:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a[21][21], i , j;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = GetRand(0, 100);
a[7][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf("%3d" , a[i][j]);
}
printf("\n");
}
return 0;
}
// random seed
int GetRand(int min, int max);
int get() {
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
// average
int avg()
float sum=0.0;
for(i = 0; i <= 21; i = i + 1) {
for(j = 0; j <= 21; j = j + 1){
sum = sum + a[21][21];
}
printf("The the average number is %.2f\n", sum/21);
}
//find maximum of all values
int *pv = &a[0][0];
max = min = 0;
for (i = 1; i < i*j; ++i){
if (pv[i] > pv[max])
max =i;
if (pv[i] < pv[min])
min = i;
}
printf("The max value is %d in row %d, col %d\n", pv[max], max/j, max%j);
return 0;
}
For the average function the compiler tells me that expected a declaration before i, which is "float sum=0.0;" but i haven't been able to fix that yet.
For the finding the max function i'm not sure yet what i'm doing there, i just have a vague idea of how it's done...am i going in the right direction?
Thanks!
It's very simple: Just assign the result of your GetRand function to the matrix entry.

Resources