int main(void)
{
    int a[] = {23, 65, 23, 3, 65, 78, 4};
    int b[] = {45, 24, 3, 87, 67, 3, 3, 5, 54};

    int index_first = arr_index_of_first_min(b, 9);
    int index_last = arr_index_of_last_min(b, 9);

    printf("---------------------------------------------------\n");
    printf("functions 1:\n");
    printf("Lowest number in array is: %d\n", arr_min(a, 7));
    printf("---------------------------------------------------\n");

    printf("---------------------------------------------------\n");
    printf("functions 2:\n");
    printf("Array index of lowest element: %d\n", index_first);
    printf("---------------------------------------------------\n");

    printf("---------------------------------------------------\n");
    printf("functions 3:\n");
    printf("Array index of last occurrence of lowest element: %d\n", index_last);
    printf("---------------------------------------------------\n");

    printf("---------------------------------------------------\n");
    printf("functions 4:\n");
    printf("Number of times min element occurs: %d\n", arr_count_min(b, 9));
    printf("---------------------------------------------------\n");

    CHECK(arr_min(a, 7) == 3);
    CHECK(arr_min(a, 7) == 4);
    CHECK(arr_index_of_first_min(b, 9) == 2);
    CHECK(arr_index_of_first_min(b, 9) == 3);

    return 0;
}
Exemple #2
0
int main(int argc, char *argv[])
{
	double arr[] = {1,2,3,-1,5.5,5.5};
	double arrTwo[] ={10,20,30,40,50};
	double *resultArr;
	double number = 5.5;
	int i, arrSize = sizeof(arr)/sizeof(double);

	printf("arr_min = %.3lf \n", arr_min(arr,arrSize));
	printf("arr_max = %.3lf \n", arr_max(arr,arrSize));
	printf("arr_average = %.3lf \n", arr_average(arr,arrSize));
	printf("arr_sum = %.3lf \n", arr_sum(arr,arrSize));
	printf("arr_contains %lf, %d times. \n", number, arr_contains(arr,arrSize,number));
	puts("Now we merge arays");
	resultArr = arr_merge(arr, arrSize, arrTwo,5);
	for (i = 0; i < arrSize +5; ++i)
	{
		printf("arr[%d] = %.3lf \n", i, resultArr[i]);
	}
	puts("After arr_clear:");
	arr_clear(arr,arrSize);
	for (i = 0; i < arrSize; ++i)
	{
		printf("arr[%d] = %.3lf \n", i, arr[i]);
	}

	return 0;

}
Exemple #3
0
int main(void) {
  int a1[] = {3, 4, 5, 6, 7, -1, -6, 19, 8, 9, 15, -4, 3, 3, 10, 11, 12};
  int a2[] = {-1, 1,  40, -2, 3, 4, -7, 4, -9, 5, 6,
              2,  -1, -1, -1, 1, 1, 1,  1, -1, 1, 1};
  int m3[] = {
      -4, 3,  2,  1,  // r1
      5,  6,  -8, -8, // r2
      33, 3,  3,  1,  // r3
      -4, -4, -4, -4, // r4
  };

  puts("Task 1");
  for (unsigned int i = 0; i < LEN(a1); i++)
    printf("%d ", a1[i]);
  puts("");
  print_n_min(a1, LEN(a1), 5);
  puts("\n");

  puts("Task 2");
  place_negatives_first(a2, LEN(a2));
  for (unsigned int i = 0; i < LEN(a2); i++)
    printf("%d ", a2[i]);
  puts("\n");

  puts("Matrix:");
  for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4; j++)
      printf("%d\t", m3[mat_to_arr(i, j, 4)]);
    puts("");
  }
  puts("");

  puts("Task 3");
  printf("Min: %d, max: %d, avg: %lf, sum: %lld, mdsum: %lld, sdsum: %lld\n\n",
         arr_min(m3, LEN(m3)), arr_max(m3, LEN(m3)), arr_avg(m3, LEN(m3)),
         arr_sum(m3, LEN(m3)), arr_diagonal_sum(m3, 4, LEN(m3)),
         arr_subdiagonal_sum(m3, 4, LEN(m3)));

  puts("Task 4");
  swap_ext_rows(m3, 4, 4);
  puts("Matrix:");
  for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4; j++)
      printf("%d\t", m3[mat_to_arr(i, j, 4)]);
    puts("");
  }
  puts("");

  return 0;
}
int main(void) {
	int a[] = { 2, 5, 0, 4 ,-1, 7, 4, 2 };
	int b[] = { 2 };
	int c[] = { 2, 5, 0, 4 ,-1, 7 };

	CHECK(arr_min(a, 8) == -1);
	CHECK(arr_min(b, 1) == -1);
	CHECK(arr_min(c, 6) == -1);

	CHECK(arr_count(a, 8, 4) == 2);
	CHECK(arr_count(b, 1, 2) == 1);
	CHECK(arr_count(c, 6, -1) == 1);

	CHECK(arr_all_even(a, 8) == 0);
	CHECK(arr_all_even(b, 1) == 1);
	CHECK(arr_all_even(c, 6) == 0);

	CHECK(arr_find_last(a, 8, 2) == 7);
	CHECK(arr_find_last(b, 1, 2) == 0);
	CHECK(arr_find_last(c, 6, 2) == 0);

	getchar();
	return 0;
}
Exemple #5
0
int main()
{   
    double arrayA[size];
    double arrayB[size];
    double searchedElement;
    int isFound;
    printf("Size of arrays is %d.\n", size);
    printf("Enter elements of arrayA\n");
    for(int i=0; i<size; i++)
    {
        scanf("%lf", &arrayA[i]);
    }  
    printf("Min element=%.2lf\n", arr_min(arrayA, size));
    printf("Max element=%.2lf\n", arr_max(arrayA, size));
    printf("Average sum of elements is %lf\n", arr_average(arrayA, size));
    printf("Sum of elements is %lf\n", arr_sum(arrayA, size));
    printf("Enter element you are looking for: ");
    scanf("%lf", &searchedElement);
    isFound=arr_contains(arrayA, size, searchedElement);
    printf(isFound? "The element is in the array\n":"The element is not in the array\n");
    printf("Enter elements of arrayB:\n");
    for(int i=0; i<size; i++)
    {
        scanf("%lf", &arrayB[i]);
    }
    double *mergedArray=arr_merge(arrayA, arrayB, size, size);
    printf("Merged array is:");
    for(int i=0; i< 2*size ; i++)
    {
        printf("%.2lf ", mergedArray[i]);
    }
    free(mergedArray);
    printf("\n");
    printf("Cleared array is: ");
    arr_clear(arrayA, size);
    for(int i=0; i<size; i++)
    {
        printf("%.2lf ", arrayA[i]);
    }
    
    return 0;
}
int main()
{
	int numbers[] = { 12, 2, 23, 41, 5, 6, 7, 8, 1 , 4};
	int length = sizeof(numbers) / sizeof(int);

	int mina = arr_min(numbers, length);
	printf("Min is: %d\n", mina);

	int max = arr_max(numbers, length);
	printf("Max is: %d\n", max);
	
	double average = arr_average(numbers, length);
	printf("Average is: %0.4lf\n", average);
	
	long sum = arr_sum(numbers, length);
	printf("Sum is: %u\n", sum);
	
	int element = 431;
	int contains = arr_contains(numbers, length, element);
	if(contains == 1)
	{
		printf("The array contains: %d\n", element);
	} else
	{
		printf("The array does not contain: %d\n", element);
	}

	int *ptr = arr_merge(numbers, length, numbers, length);
	printf("%d == %d %d == %d \n", ptr[0], ptr[10], ptr[1], ptr[11]);
	free(ptr);

	arr_clear(numbers, length);
	printf("%d %d\n", numbers[0], numbers[1]);

	return 0;
}
int ws_inv_cmod5(double sigma0, double phi0, double theta0,
                 double *wnd1, double *wnd2,
                 double min_ws, double max_ws, int npts,
                 double hh)
{
  // ws_cmod5 will return a 2-element array where the first element is the sigma0
  // that you'd get at min_ws, and the second element is the sigma0 that you get
  // at max_ws;
  // (phi0 is phi_diff, the diff between wind_dir and r_look, and theta0 is incidence angle)
  // Note that the 0.0 (last parameter) is r_look according to ws_cmod5, but the ws_cmod5 function
  // doesn't make use of it ...or more accurately, it's already built into the phi0 value.
  g_assert(max_ws > min_ws);
  g_assert(npts > 0);
  double *wd;
  int i;

  // Make an npts-element array of windspeeds that range from min_ws to max_ws linearly
  wd = maken(min_ws, max_ws, npts);
  g_assert(wd != NULL);

  // Calculate an npts-element array of normalized radar cross section (NRCS) using
  // the CMOD5 algorithm
  double *sg0 = ws_cmod5(wd, npts, phi0, theta0, 0.0); // Returned sg0[] has npts elements in it
  g_assert(sg0 != NULL);

  // See lines 118-123 in ws_inv_cmod5.pro
  // NOTE: The CMOD5 and CMOD4 algorithms need an adjustment when the polarization
  // is HH (since the original algorithms were developed for VV polarization only)
  // hh == -3 when polarization is VV, hh == 0.6 when polarization is HH
  // FIXME: Add cases for hh eq to -1 and -2
  double rr = (hh > 0.0) ? ws_pol_ratio(theta0, hh) : 1.0;
  sigma0 = (hh != -3) ? sigma0 / rr : sigma0; // HH adjustment or not

  // If sigma0 is lower than what you'd get at minimum windspeed, then set the windspeed to
  // the minimum and return. (Line 129)
  double min_nrcs = arr_min(sg0, npts); // Min should be sg0[0], but make sure...
  if (sigma0 < min_nrcs) {
    *wnd1 = wd[0];
    *wnd2 = WND1_IS_MINIMUM_WIND;
    FREE(wd);
    FREE(sg0);
    return 0;
  }

  // Create sign of differences array
  int ct=0;
  double *s = (double *)MALLOC(sizeof(double) * npts);
  for (i=0; i<npts; i++) {
    s[i] = SIGN(sg0[i] - sigma0);
    ct += s[i] == 0 ? 1 : 0;
    s[i] = (ct > 0 && s[i] == 0) ? 1.0 : s[i];
  }

  // Count the sign changes
  ct = 0;
  int ww = -1; // Where first sign change occurs
  int ww2 = -1; // Where second sign change occurs
  for (i=1; i<npts; i++) {
    ct += (s[i] != s[i-1]) ? 1 : 0;
    ww = (ct > 0 && ww < 0) ? i : ww;
    ww2 = (ct > 0 && ww > 0 && ww2 < 0) ? i : ww2;
  }

  // Calculate wind for the 3 cases (no sign changes, one sign change, two sign changes, and other)
  switch(ct) {
    case 0:
      {
        // No sign changes means sigma0 > max(sg0[])
        int nn = npts;
        double max_sg0 = arr_max(sg0, npts);
        int idx_first_max = -1;
        int *w = (int *)CALLOC(nn, sizeof(int));
        int wx = 0;
        for (i=0; i<npts; i++) {
          if (sg0[i] >= max_sg0) {
            idx_first_max = (idx_first_max < 0) ? i : idx_first_max;
            w[wx++] = i; // Collect indices of max's
          }
        }
        if (idx_first_max == nn - 1) {
          // Last element was the largest element, so send back max wind
          *wnd1 = wd[nn-1];
          *wnd2 = WND_FROM_MAX_SIGMA0;
        }
        else {
          int ix1 = (w[0] - 1 >= 0) ? w[0] - 1 : 0;
          int ix2 = w[0];
          int ix3 = w[0] + 1;
          double fit1;
          double *f1 = poly_fit(wd, sg0, ix1, ix2, ix3, 2, &fit1);
          *wnd1 = (f1[1]+sqrt(f1[1]*f1[1]-4.0*f1[2]*(f1[0]-sg0[ix2])))/2/f1[2];
          *wnd2 = WND_FROM_MAX_SIGMA0;
        }
        FREE(w);
      }
      break;
    case 1:
      {
        // Single solution
        int ix1 = ww;
        int ix2 = (ww+1) > npts - 1 ? npts - 1 : ww+1;
        int ix3 = (ww+2) > npts - 1 ? npts - 1 : ww+2;
        double fit1;
        double *f1 = poly_fit(wd, sg0, ix1, ix2, ix3, 2, &fit1);
        *wnd1 = (f1[1]+sqrt(f1[1]*f1[1]-4.0*f1[2]*(f1[0]-sigma0)))/2/f1[2];
        *wnd2 = WND1_IS_ONLY_SOLUTION;
      }
      break;
    case 2:
      {
        // Two solutions (usually lowest answer of the two is best answer)
        int ix1 = ww;
        int ix2 = (ww+1) > npts - 1 ? npts - 1 : ww+1;
        int ix3 = (ww+2) > npts - 1 ? npts - 1 : ww+2;
        int ix4 = ww2;
        int ix5 = (ww2+1) > npts - 1 ? npts - 1 : ww2+1;
        int ix6 = (ww2+2) > npts - 1 ? npts - 1 : ww2+2;
        double fit1, fit2;
        double *f1 = poly_fit(wd, sg0, ix1, ix2, ix3, 2, &fit1);
        double *f2 = poly_fit(wd, sg0, ix4, ix5, ix6, 2, &fit2);
        *wnd1 = (f1[1]+sqrt(f1[1]*f1[1]-4.0*f1[2]*(f1[0]-sigma0)))/2/f1[2];
        *wnd2 = (f2[1]+sqrt(f2[1]*f2[1]-4.0*f2[2]*(f2[0]-sigma0)))/2/f2[2];
      }
      break;
    default:
      *wnd1 = WND_FROM_MAX_SIGMA0;
      *wnd2 = WND_FROM_MAX_SIGMA0;
      break;
  }

  FREE(s);
  FREE(wd);
  FREE(sg0);

  return 0;
}
int main()
{
    int n;
    if (scanf("%d", &n) != 1)
    {
        printf("Invalid input!");
        return 1;
    }

    double numbers[n];
    int i;
    for (i = 0; i < n; i++)
    {
        scanf("%lf", &numbers[i]);
    }

    int length = lengthArr(numbers);
    int zeroFraction = count_of_zero_fractions(numbers, length);
    int notZeroFraction = length - zeroFraction;
    double fractioned[notZeroFraction];
    double zeroFractioned[zeroFraction];
    int j= 0, k = 0;
    for (i = 0; i < n; i++)
    {
        if (fmod(numbers[i], 1) == 0)
        {
            zeroFractioned[j] = numbers[i];
            j++;
        }
        else
        {
            fractioned[k] = numbers[i];
            k++;
        }
    }
    double fractionedMin = arr_min(fractioned, notZeroFraction);
    double zeroFractionedMin = arr_min(zeroFractioned, zeroFraction);

    double fractionedMax = arr_max(fractioned, notZeroFraction);
    double zeroFractionedMax = arr_max(zeroFractioned, zeroFraction);

    double fractionedSum = arr_sum(fractioned, notZeroFraction);
    double zeroFractionedSum = arr_sum(zeroFractioned, zeroFraction);

    double fractionedAvg = arr_average(fractioned, notZeroFraction);
    double zeroFractionedAvg = arr_average(zeroFractioned, zeroFraction);

    printf("\n");
    print_array(fractioned, notZeroFraction);
    printf(" -> min: %.3lf", fractionedMin);
    printf(", max: %.3lf", fractionedMax);
    printf(", sum: %.3lf", fractionedSum);
    printf(", avg: %.2lf", fractionedAvg);
    printf("\n");
    print_array(zeroFractioned, zeroFraction);
    printf(" -> min: %.lf", zeroFractionedMin);
    printf(", max: %.lf", zeroFractionedMax);
    printf(", sum: %.lf", zeroFractionedSum);
    printf(", avg: %.2lf", zeroFractionedAvg);

    printf("\n");

    return 0;
}