Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Zadanie 1
- #include <stdlib.h>
- #include <stdio.h>
- struct particle
- {
- int mass;
- int speed;
- int momentum
- };
- typedef struct particle part;
- void printParticle(part* p)
- {
- printf("mass: %i\n", p->mass);
- printf("speed: %i\n", p->speed);
- }
- void readParticle(part* p)
- {
- printf("Please enter the data for a new particle.\n");
- do {
- printf("mass: ");
- scanf("%i",&p->mass);
- }while(p->mass <= 0);
- printf("speed: ");
- scanf("%i",&p->speed);
- }
- void momentum(part* p){
- p->momentum = p->mass*p->speed;
- }
- void printMomentum(part* p){
- printf("momentum: %i\n", p->momentum);
- }
- int main()
- {
- int i;
- int dim = 0;
- do {
- printf("How many particles do you want to have? ");
- scanf("%i",&dim);
- } while(dim <= 0);
- part* p;
- p = (part*) malloc(dim*sizeof(part));
- if (p==NULL) {
- printf("Problem with memory!\n");
- return EXIT_FAILURE;
- }
- for (i=0; i<dim; i++)
- {
- readParticle(&p[i]);
- }
- printf("\n\n");
- /*printf("The following particles\n");
- for (i=0; i<dim; i++)
- {
- printParticle(&p[i]);
- }
- */
- for (i=0; i<dim; i++)
- {
- momentum(&p[i]);
- }
- int n = p[0].momentum;
- for( i = 0; i < dim; i++){
- if ( p[i].momentum < n){
- n = p[i].momentum;
- }
- }
- printf("\nSmallest momentum is %i",n);
- free(p);
- return EXIT_SUCCESS;
- }
- //Zadanie 2
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main()
- {
- srand (time(NULL));
- int dim,i;
- do{
- printf("Please, type the dimension of the vector: ");
- scanf("%i",&dim);
- }while(dim < 0);
- int* vec = (int*) malloc(dim*sizeof(int));
- if (vec == NULL)
- {
- printf("ERROR: not enough memory!\n");
- return EXIT_FAILURE;
- }
- for( i = 0; i < dim; i++){
- vec[i] = rand() % 10 + 1;
- }
- for ( i = 0; i < dim; i++){
- printf("vec[%i] = %i\n",i,vec[i]);
- }
- int count = 0;
- for (i = 0; i< dim; i++){
- if(vec[i] > 8){
- count++;
- }
- }
- printf("%i vectors are larger than 8",count);
- free(vec);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement