Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* program ktory utworzy tab losowe od a do , funkcje ktora utworzy nowa tablice zawierajaca po jednym wystapieniu elementu z tab 1*/
- int x;
- int* new_tab(int n)
- {
- int *p=(int*)malloc(n*sizeof(int));
- return p;
- }
- void sort_tab(int *tab,int n)
- { int x=0;
- for(int i=0;i<n;i++)
- {
- for (int j=0;j<n-1;j++)
- {
- if(tab[j]>tab[j+1])
- {
- x=tab[j];
- tab[j]=tab[j+1];
- tab[j+1]=x;
- }
- }
- }
- }
- int* tabex(int *tab,int n)
- {
- x=1;
- int k=0;
- for(int i=0;i<n-1;i++)
- if(tab[i]!=tab[i+1])x++;
- int *tab2=new_tab(x);
- for(int i=0;i<n;i++)
- {
- if(tab[i]!=tab[i+1] || tab[i]==tab[n])
- {
- tab2[k]=tab[i];
- k++;
- }
- }
- return tab2;}
- void print_tab(int*tab,int n)
- {
- for(int i=0;i<n;i++)
- printf("%d ",tab[i]);
- printf("\n");
- }
- int main()
- { srand(time(NULL));
- int i,n,a,b;
- printf("rozmiar tablicy");
- scanf("%d",&n);
- printf("podaj zakres losowanie");
- scanf("%d%d",&a,&b);
- int* tab1=new_tab(n);
- for(i=0;i<n;i++)tab1[i]=rand()%(b-a+1)+a;
- print_tab(tab1,n);
- sort_tab(tab1,n);
- int *tab2=tabex(tab1,n);
- print_tab(tab2,x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement