Why not malloc the array is full?

#include <locale.h>
#include <stdio.h>
#include <time.h>
#include <malloc.h>
#include <stdlib.h>
#define N 1000
int sort(int mas_otr, int n);

int main()

{
 setlocale(LC_ALL, "RUS"); 
 int i, n, *mas, *mas_otr;
 setlocale(0, "rus");
 printf("Enter size of array: ");
 scanf("%d", &n);
srand(time(0));
 mas = malloc(n * sizeof(int));
 mas_otr = malloc(n * sizeof(int));
 printf("Generated array:\n");
 for (i = 0; i < n; i++) {
 mas[i] = rand() % 100;
 printf("%d ", mas[i]);
}
 for (int b = 0; b < n; n++) {
 if (mas[b] < 0) { mas_otr[b] = mas[b]; printf("%d", mas_otr[b]); //OUTPUTS the ARRAY
}
}
sort(mas_otr,n);
free(mas);
free(mas_otr);
 return 0;

 getchar(); 

}

int sort(int mas_otr, int n) {
 int i, *j, tmp;
 for (i = 0; i < n; i++) { 
 for (j = 0; j < n - i - 1; j++) {
 if (mas_otr[j] > mas_otr[j + 1]) {
 tmp = mas_otr[j];
 mas_otr[j] = mas_otr[j + 1];
 mas_otr[j + 1] = tmp;
}
}
}
 puts("Array with negative values");
 for (int c = 0; c < n; c++) {
 printf("%d", mas_otr[c]); //OUTPUTS the ARRAY
}
}
April 4th 20 at 13:09
1 answer
April 4th 20 at 13:11
1.rand() generates only positive numbers: https://en.cppreference.com/w/c/numeric/random/rand
2.Even if claim 1 would have been all right after the allocated memory for mas_otr it needs to be reset, because no malloc initialize the memory does not produce or it will contain garbage. And you fill mas_otr only selectively. So the final result is obviously the same will not be what you expect.
3.need in the right places to bring the line breaks, and now you all will cling to one line.
4.In sort() instead of an array you pass in an integer.
5.deal with sort() you've confused with pointers and indexes.
i,j,c must be an int
mas_otr - int* - Gina.Volkman32 commented on April 4th 20 at 13:14
How to consistently fill the array? Now all of ratchet created but not populated.
#include <locale.h>
#include <stdio.h>
#include <time.h>
#include <malloc.h>
#include <stdlib.h>
#define N 1000
int sort(int mas_otr, int n);

int main()

{
 setlocale(LC_ALL, "RUS"); 
 int i, n, *mas, *mas_otr,l=0;
 setlocale(0, "rus");
 printf("Enter size of array: ");
 scanf("%d", &n);
srand(time(0));
 mas = malloc(n * sizeof(int));

 printf("Generated array:\n");
 for (i = 0; i < n; i++) {
 mas[i] = rand() % 100 - 32;
 printf("%d ", mas[i]);
}
 for (int b = 0; b < n; n++) {
 if (mas[b] < 0) {
l++;
}
}
 mas_otr = malloc(l * sizeof(int));
 for (int b = 0; b < n; n++) {
 if (mas[b] < 0) { mas_otr[b] = mas[b]; //it must fill in the elements of the new array 
}
}
sort(mas_otr,n);
free(mas);
free(mas_otr);
 return 0;

 getchar(); 

}

int sort(int mas_otr, int n) {
 int i, *j, tmp;
 for (i = 0; i < n; i++) { 
 for (j = 0; j < n - i - 1; j++) {
 if (mas_otr[j] > mas_otr[j + 1]) {
 tmp = mas_otr[j];
 mas_otr[j] = mas_otr[j + 1];
 mas_otr[j + 1] = tmp;
}
}
}
 puts("Array with negative values");
 for (int c = 0; c < n; c++) {
 printf("%d", mas_otr[c]);
}


}
- Carmine commented on April 4th 20 at 13:17
Cool, that in the current combination of types j and mas_otr in the first cycle sort() all works correctly (only for x32 version of the Assembly).
But this is only due to the fact that you are lucky so make a mistake. Hardly meant what is written. So you have to fix it. - Gina.Volkman32 commented on April 4th 20 at 13:20
@Carmine,
In mas_otr[b] = mas[b] - exit the array bounds if b > l - Gina.Volkman32 commented on April 4th 20 at 13:23
@Carmine,
Now all of ratchet created but not populated.

Because b is the index for the array mas, in mas_otr needs its own index in the third cycle main().
You still apparently the debugger has not learned to use it? It's time! - Gina.Volkman32 commented on April 4th 20 at 13:26
@Gina.Volkman32,
for (int b = 0; b < n; b++) {
 if (mas[b] < 0) { mas_otr[n-b] = mas[b];
 }
- Carmine commented on April 4th 20 at 13:29
@Gina.Volkman32, He needs first to fill. Now I have written!)))))))))))))))))))))))) - Carmine commented on April 4th 20 at 13:32
@Gina.Volkman32,
#include <locale.h>
#include <stdio.h>
#include <time.h>
#include <malloc.h>
#include <stdlib.h>
#define N 1000
int sort(int mas_otr, int n);

int main()

{
 setlocale(LC_ALL, "RUS"); 
 int i, n, *mas, *mas_otr,l=0;
 setlocale(0, "rus");
 printf("Enter size of array: ");
 scanf("%d", &n);
 mas = malloc(n * sizeof(int));

 printf("Generated array:\n");
 for (i = 0; i < n; i++) {
 mas[i] = rand() % 100 - 32;
 printf("%d ", mas[i]);
}
 for (int b = 0; b < n; n++) {
 if (mas[b] < 0) {
l++;
}
}
 mas_otr = malloc(l * sizeof(int));
 for (int b = 0; b < n; b++) {
 if (mas[b] < 0) { mas_otr[n-b] = mas[b];
}
}
sort(*mas_otr,l);
free(mas);
free(mas_otr);
 return 0;

 getchar(); 

}

int sort(int *mas_otr, int l) {
 int i, j, tmp;
 for (i = 0; i < l; i++) { 
 for (j = 0; j < l - i - 1; j++) {
 if (mas_otr[j] > mas_otr[j + 1]) {
 tmp = mas_otr[j];
 mas_otr[j] = mas_otr[j + 1];
 mas_otr[j + 1] = tmp;
}
}
}
 puts(" \n Array with negative values");
 for (int c = 0; c < l; c++) {
 printf("%d", mas_otr[c]);
}


}
- Carmine commented on April 4th 20 at 13:35
@Gina.Volkman32, yeah, a lot of errors. Correct ) - Carmine commented on April 4th 20 at 13:38
for (int b = 0, c = 0; b < n; b++) {
 if (mas[b] < 0) { 
 mas_otr[c] = mas[b]; 
c++;
 }
- Gina.Volkman32 commented on April 4th 20 at 13:41
@Gina.Volkman32, thank you! - Carmine commented on April 4th 20 at 13:44

Find more questions by tags C