SHARE
TWEET

zadanko1

a guest Jun 16th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void fill(unsigned int** tabelka, int x, int y) {
  5.     for(int i = 0; i < x; i++) {
  6.         for(int k = 0; k < y; k++) {
  7.             scanf("%u", &tabelka[i][k]);
  8.         }
  9.     }
  10. }
  11.  
  12. int oblicz_zera_binarne(unsigned int value) {
  13.     int zera = 0;
  14.     while(value > 0) {
  15.         if(value % 2 == 0) {
  16.             zera++;
  17.         }
  18.         value = value >> 1;
  19.     }
  20.     return zera;
  21. }
  22. //y - kolumn, x - wiersze
  23. unsigned int* znajdz_wartosci_dla_kolumny(unsigned int** tabelka, int x, int y) {
  24.     int* wartosci_dla_kolumn = (int*) malloc(y * sizeof(int));
  25.  
  26.     for(int i = 0; i < y; i++) {
  27.         wartosci_dla_kolumn[i] = 0;
  28.  
  29.         for(int k = 0; k < x; k++) {
  30.             wartosci_dla_kolumn[i] += oblicz_zera_binarne(tabelka[k][i]);
  31.         }
  32.     }
  33.    
  34.     int najwiekszy_indeks = 0;
  35.     int max = 0;
  36.     for(int i = 0; i < y; i++) {
  37.         if(wartosci_dla_kolumn[i] > max) {
  38.             najwiekszy_indeks = i;
  39.             max = wartosci_dla_kolumn[i];
  40.         }
  41.     }
  42.  
  43.     unsigned int* wartosc_finalne = (unsigned int*) malloc(x * sizeof(unsigned int));
  44.     for(int i = 0; i < x; i++) {
  45.         wartosc_finalne[i] = tabelka[i][najwiekszy_indeks];
  46.     }
  47.  
  48.     return wartosc_finalne;
  49. }
  50.  
  51. int main() {
  52.     int x, y;
  53.     printf("Podaj liczbe wierszy: ");
  54.     scanf("%d", &x);
  55.     printf("Podaj liczbe kolumn: ");
  56.     scanf("%d", &y);
  57.  
  58.     unsigned int** tabelka = (unsigned int**)malloc(sizeof(unsigned int*) * x);
  59.     for(int i = 0; i < x; i++) {
  60.         tabelka[i] = (unsigned int*) malloc(sizeof(unsigned int) * y);
  61.     }
  62.     printf("Wprowadz elementy do tabelki: ");
  63.     fill(tabelka, x, y);
  64.  
  65.     unsigned int* rezultat = znajdz_wartosci_dla_kolumny(tabelka, x, y);
  66.     printf("Wartosci:\n");
  67.     for(int i = 0; i < x; i++) {
  68.         printf("%u\n", rezultat[i]);
  69.     }
  70.  
  71.     return 0;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top