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.
Related
Trying to solve https://leetcode.com/problems/k-concatenation-maximum-sum/submissions/, I still got integer overflow when the data type is long
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int max(int a, int b) {
return a > b ? a : b;
}
int sum(int *nums, int numSize) {
int ans = 0;
for (int i = 0; i < numSize; ++i)
ans += nums[i];
return ans;
}
int KandaneAlgo(int *nums, int numSize) {
int i, max_overall_so_far = 0, max_ending_here = 0;
for (i = 0; i < numSize; ++i) {
max_ending_here += nums[i];
if (max_ending_here < 0)
max_ending_here = 0;
if (max_overall_so_far < max_ending_here)
max_overall_so_far = max_ending_here;
}
return max_overall_so_far;
}
int kConcatenationMaxSum(int *arr, int arrSize, int k) {
int mod = pow(10, 9) + 7;
int ans;
if (k > 1) {
long tem = (k - 2) * max(sum(arr, arrSize), 0);
int t1 = tem % mod;
printf("%ld, %d, %d \n", tem, t1, mod);
int arr2[2 * arrSize];
for (int i = 0; i < 2 * arrSize; ++i)
arr2[i] = arr[i - i / arrSize * arrSize];
ans = (int)t1 + KandaneAlgo(arr2, 2 * arrSize) % mod;
} else {
ans = KandaneAlgo(arr, arrSize) % mod;
}
return ans;
}
int main() {
int arr[10] = { [0 ... 9] = 10000 };
for (int i = 0; i < 10; ++i)
printf("%d ", arr[i]);
printf("\n");
int tot = kConcatenationMaxSum(arr, 10, 100000);
printf("%d \n", tot);
return 0;
}
I locally debug with lldb and can see the output message of variable tem is wrong indeed, it should be 9999800000.
This is because 9999800000 is larger than what can be stored in 32 bits. long only provides a minimum size guarantee of 32 bits. If you use long long for all the operands and result variable in the expression, it evaluates to the correct value. long long provides a minimum guarantee of 64 bits.
Check this for more details - https://en.wikipedia.org/wiki/C_data_types#Main_types
The following snippet worked for me:
long long tem = (long long)(k-2) * (long long)max(sum(arr, arrSize), 0);
Not sure about the rest of the algorithm, but this puts the correct value into tem.
So, I tried to implement the Middle Square PRNG method, to generate the first 100 numbers. It works well until a certain point, when I get as a result negative numbers.
I used the time library to change the values on my temp array, so that it won't get stuck on the same sequence, where the number ends with two zeros.
My code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
time_t now;
struct tm *tm;
unsigned long int prng(int seed)
{
int num = seed * seed;
int t[10], inc = 0;
//Reverse number in an array
while(num != 0)
{
t[inc] = num%10;
num /= 10;
inc++;
}
int min = inc/4;
int max = inc / 2 + min;
int temp[10];
//Assign the middle square part to another table
for(int i = min; i <= max; i++)
{
temp[i-min] = t[i];
}
for(int i=0; i < max-min; i++)
{
//Check if there is a "0" "0" sequence, if TRUE - replace with current time (seconds)
if(temp[i] == 0 && temp[i+1] == 0)
{
now = time(0);
tm = localtime(&now);
temp[i] = tm->tm_sec/10;
temp[i + 1] = tm->tm_sec%10;
}
}
//Transform the squared array into an integer
unsigned long int k = 0;
for (int i = 0; i <= max-min; i++)
k = 10 * k + temp[i];
return k;
}
int main()
{
unsigned long int n = 123; //seed
printf("%d ", n);
for(int i = 0; i<100; i++)
{
n = prng(n);
printf("\n%d ", n);
}
return 0;
}
The results that I get:
123
215
226
701
419
6557
24992
7064
7099
85930
-696950
8997
6490
10212
94824
36561
760763
-724206
30238
66334
22325
65048
-94273
...
I tackled the problem by first figuring out the length of two given numbers and aligning the one with less digits (if one exists) into a new array so that the ones, tens, hundreds etc. align with the bigger number's ones, tens, hundreds, etc.
Then I wanted to save the sum of each two aligned elements (with a mod of 10) into a new array while checking if the sum of digits is greater than 10 - just the basic sum stuff. Now the problem occurs with adding two elements into the aplusb integer and I've tried fixing it with writing
int aplusb = (lengthA[max-i]-'0') +(temp[max-i]-'0');
but it doesn't work. I'm stuck and I don't know what to do. Please help.
The whole code:
#include <stdio.h>
#include <math.h>
int main(){
char a[10000];
char b[10000];
scanf("%s %s", &a, &b);
char sum[10000];
int lengthA = 0;
int lengthB = 0;
int i = 0;
while(a[i]){
i++;
} lengthA = i;
i = 0;
while(b[i]){
i++;
} lengthB = i;
char temp[10000];
int aplusb;
int carry = 0;
int max = lengthA;
int difference = abs(lengthA - lengthB);
if(lengthA>lengthB){
for(i=0; i<lengthA; i++){
temp[i+difference]=b[i];
}
for(i=0; i<=max; i++){
aplusb = lengthA[max-i]+temp[max-i]; //<-- this is the problematic line
if(carry = 1) aplusb++;
if(aplusb>9){
carry = 1;
aplusb%=10;
}
sum[i]=aplusb;
}
}
for(i=0; i<=max; i++){
printf("%c", sum[i]);
}
/*
if(lengthB>lengthA){
max = lengthB;
for(i=0; i<lengthB; i++){
temp[i+difference]=a[i];
}
}*/
return 0;
}
Doing operations and storing on very large numbers is very akin to doing operations and storing polynomials, i.e. with x = 10. a0 + a1.10 + a2.10^2 ... + an.10^n.
There are many polynomial libraries on the Internet, where you could find inspiration. All operations on your very large numbers can be expressed in terms of polynomials. This means that by using base 2^8, or even base 2^63, instead of base 10 to internally store your large numbers you would greatly improve performance.
You must also normalize your coefficients after operations to keep them positive. Operations may result in a negative coefficient, That can easily be fixed, as it is very similar to borrowing after a subtraction, this means coefficients must be larger than your base by 1bit.
To convert back to base 10, you'd need to solve r (your result) for v (your value), such as r(10)=v(2^63). This has only one solution, if you enforce the positive coefficients rule.
[note] After thinking about it some more: the rule on positive coefficients may only be necessary for printing, after all.
Example: adding. no memory error checking
int addPolys(signed char** result, int na, const signed char* a, int nb, const signed char* b)
{
int i, nr, nmin, carry, *r;
nr = max(na, nb) + 1;
nmin = min(na, nb);
r = malloc(sizeof(signed char) * (na + nb + 1));
if (nb < na)
{
nr = nb;
}
for (i = 0; i < nmin; ++i)
{
r[i] = a[i] + b[i];
}
for (; i < na; ++i)
{
r[i] = a[i];
}
for (; i < nb; ++i)
{
r[i] = b[i];
}
r[nr - 1] = 0;
// carry - should really be a proc of its own, unoptimized
carry = 0;
for (i = 0; i < nr; ++i)
{
r[i] += carry;
if (r[i] > 10)
{
carry = r[i] / 10;
r[i] %= 10;
}
else if (r[i] < 0)
{
carry = (r[i] / 10) - 1;
r[i] -= (carry * 10);
}
else
carry = 0;
}
// 'remove' leading zeroes
for (i = nr - 1; i > 0; --i)
{
if (r[i] != 0) break;
}
++i;
*result = r;
if (i != nr)
{
*result = realloc(i * sizeof(signed char));
}
return i; // return number of digits (0 being 1 digit long)
}
That code is working now for any two positive numbers with up to ten thousand digits:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(){
char chara[10000];
char charb[10000];
scanf("%s %s", &chara, &charb);
int lengthA = strlen(chara);
int lengthB = strlen(charb);
int max = lengthA;
if(lengthB>lengthA) max=lengthB;
int dif = abs(lengthA - lengthB);
//ustvari int tabele
int a[max];
int b[max];
int sum[max+1];
// nastavi nule
int i;
for(i=0; i<max; i++){
a[i] = 0;
b[i] = 0;
sum[i] = 0;
} sum[max] = 0;
//prekopiraj stevila iz char v int tabele &obrni vrstni red
for(i=0; i<lengthA; i++){
a[i] = chara[lengthA-i-1]-'0';
}
for(i=0; i<lengthB; i++){
b[i] = charb[lengthB-i-1]-'0';
}
int vsota;
int prenos = 0;
for(i=0; i<max; i++){
vsota = a[i]+b[i] + prenos;
if(vsota>=10) prenos = 1;
else if (vsota<10) prenos = 0;
sum[i]=vsota%10;
}
if(prenos==1){
sum[max] = 1;
for(i = max; i>=0; i--){
printf("%d", sum[i]);
}
} else {
for(i = max-1; i>=0; i--){
printf("%d", sum[i]);
}
}
return 0;
}
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
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.