Finding the number of negative elements in columns of certain zone and sorting that columns












0















The problem is that function "sort" doesn`t work propably. It should find the number of negative elements in columns of certain zone(look down)
zone

then it shold sort columns with the number of that negativee elements.
Could you please help me and show my mistakes?



#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

void randInit(int *a, int power);
void array_output(int *array, int power);
double avarage_sum(int *array, int power);
void sort(int *array, int power);


int main() {
srand(time(NULL));
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
printf("Введіть розмір квадратної матриці матриці:n");
int m;
scanf_s("%d", &m);
int *a = (int*)malloc(m*m * sizeof(int));
randInit(a, m);
array_output(a, m);
printf("nОсь середнє значення заданих елементів:
%.2lf",avarage_sum(a,m));
sort(a, m);
_getch();
return 0;
}

void randInit(int *a, int power) {
srand(time(NULL));

for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++) {
*(a + i * power + j) = -20 + rand() % 40;
}
}
}

void array_output(int *array, int power) {
for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++)
printf("%dt", *(array + i * power + j));
printf("n");
}
}

double avarage_sum(int *array, int power) {
int k = 0;
int n = 0;
double sum = 0.0;
for (int i = 0; i < (power / 2); i++) {
for (int j = i; j < power - k; j++) {
sum += *(array + i * power + j);
n++;
}
k++;
}
k = 0;
for (int i = power - 1; i > (power / 2); i--) {
for (int j = power - k - 1; j > k - 1; j--) {
sum += *(array + i * power + j);
n++;
}
k++;
}
if (power % 2 == 0)
sum;
else {
sum += *(array + (power*(power / 2)) + (power / 2));
n++;
}

return sum / n;
}

void sort(int *array, int power) {
int k = 0;
int *negative_number = (int*)calloc(power * 2, sizeof(int));

for (int j = 0; j < power; j++) {
if (k < power / 2) {
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k++;
}
else
{
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k--;
}
*(negative_number + 2 * j + 1) = j;
}
if (power / 2 != 0)
if (*(negative_number + power - 1) < 0)
*(negative_number + power - 1) -= 1;
printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));

for (int i = 0; i < power * 2 - 1; i++) {
for (int j = 0; j < power * 2 - 2 * i - 1; j++) {
if (*(negative_number + 2 * j) > *(negative_number + 2 * j + 2))
{
int tmp_mean = *(negative_number + 2 * j);
int tmp_index = *(negative_number + 2 * j + 1);
*(negative_number + 2 * j) = *(negative_number + 2 * j + 2);
*(negative_number + 2 * j + 2) = tmp_mean;
*(negative_number + 2 * j + 1) = *(negative_number + 2 * j +
3);
*(negative_number + 2 * j + 3) = tmp_index;
}
}
}

printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));
/*for(int j=0;j<power;j++){
for(int i=0;i<power;i++){
printf("%d ", *(array + *(negative_number + 2 * j + 1) * power +
i));
}
printf("n");
}
*/
}









share|improve this question




















  • 1





    C tip for the day: you can do cast input pointer into 2d array with int (*array2d)[power] = (void*)array; in your functions and then simply access with like array2d[i][j].

    – Kamil Cuk
    Nov 25 '18 at 16:38
















0















The problem is that function "sort" doesn`t work propably. It should find the number of negative elements in columns of certain zone(look down)
zone

then it shold sort columns with the number of that negativee elements.
Could you please help me and show my mistakes?



#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

void randInit(int *a, int power);
void array_output(int *array, int power);
double avarage_sum(int *array, int power);
void sort(int *array, int power);


int main() {
srand(time(NULL));
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
printf("Введіть розмір квадратної матриці матриці:n");
int m;
scanf_s("%d", &m);
int *a = (int*)malloc(m*m * sizeof(int));
randInit(a, m);
array_output(a, m);
printf("nОсь середнє значення заданих елементів:
%.2lf",avarage_sum(a,m));
sort(a, m);
_getch();
return 0;
}

void randInit(int *a, int power) {
srand(time(NULL));

for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++) {
*(a + i * power + j) = -20 + rand() % 40;
}
}
}

void array_output(int *array, int power) {
for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++)
printf("%dt", *(array + i * power + j));
printf("n");
}
}

double avarage_sum(int *array, int power) {
int k = 0;
int n = 0;
double sum = 0.0;
for (int i = 0; i < (power / 2); i++) {
for (int j = i; j < power - k; j++) {
sum += *(array + i * power + j);
n++;
}
k++;
}
k = 0;
for (int i = power - 1; i > (power / 2); i--) {
for (int j = power - k - 1; j > k - 1; j--) {
sum += *(array + i * power + j);
n++;
}
k++;
}
if (power % 2 == 0)
sum;
else {
sum += *(array + (power*(power / 2)) + (power / 2));
n++;
}

return sum / n;
}

void sort(int *array, int power) {
int k = 0;
int *negative_number = (int*)calloc(power * 2, sizeof(int));

for (int j = 0; j < power; j++) {
if (k < power / 2) {
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k++;
}
else
{
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k--;
}
*(negative_number + 2 * j + 1) = j;
}
if (power / 2 != 0)
if (*(negative_number + power - 1) < 0)
*(negative_number + power - 1) -= 1;
printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));

for (int i = 0; i < power * 2 - 1; i++) {
for (int j = 0; j < power * 2 - 2 * i - 1; j++) {
if (*(negative_number + 2 * j) > *(negative_number + 2 * j + 2))
{
int tmp_mean = *(negative_number + 2 * j);
int tmp_index = *(negative_number + 2 * j + 1);
*(negative_number + 2 * j) = *(negative_number + 2 * j + 2);
*(negative_number + 2 * j + 2) = tmp_mean;
*(negative_number + 2 * j + 1) = *(negative_number + 2 * j +
3);
*(negative_number + 2 * j + 3) = tmp_index;
}
}
}

printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));
/*for(int j=0;j<power;j++){
for(int i=0;i<power;i++){
printf("%d ", *(array + *(negative_number + 2 * j + 1) * power +
i));
}
printf("n");
}
*/
}









share|improve this question




















  • 1





    C tip for the day: you can do cast input pointer into 2d array with int (*array2d)[power] = (void*)array; in your functions and then simply access with like array2d[i][j].

    – Kamil Cuk
    Nov 25 '18 at 16:38














0












0








0








The problem is that function "sort" doesn`t work propably. It should find the number of negative elements in columns of certain zone(look down)
zone

then it shold sort columns with the number of that negativee elements.
Could you please help me and show my mistakes?



#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

void randInit(int *a, int power);
void array_output(int *array, int power);
double avarage_sum(int *array, int power);
void sort(int *array, int power);


int main() {
srand(time(NULL));
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
printf("Введіть розмір квадратної матриці матриці:n");
int m;
scanf_s("%d", &m);
int *a = (int*)malloc(m*m * sizeof(int));
randInit(a, m);
array_output(a, m);
printf("nОсь середнє значення заданих елементів:
%.2lf",avarage_sum(a,m));
sort(a, m);
_getch();
return 0;
}

void randInit(int *a, int power) {
srand(time(NULL));

for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++) {
*(a + i * power + j) = -20 + rand() % 40;
}
}
}

void array_output(int *array, int power) {
for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++)
printf("%dt", *(array + i * power + j));
printf("n");
}
}

double avarage_sum(int *array, int power) {
int k = 0;
int n = 0;
double sum = 0.0;
for (int i = 0; i < (power / 2); i++) {
for (int j = i; j < power - k; j++) {
sum += *(array + i * power + j);
n++;
}
k++;
}
k = 0;
for (int i = power - 1; i > (power / 2); i--) {
for (int j = power - k - 1; j > k - 1; j--) {
sum += *(array + i * power + j);
n++;
}
k++;
}
if (power % 2 == 0)
sum;
else {
sum += *(array + (power*(power / 2)) + (power / 2));
n++;
}

return sum / n;
}

void sort(int *array, int power) {
int k = 0;
int *negative_number = (int*)calloc(power * 2, sizeof(int));

for (int j = 0; j < power; j++) {
if (k < power / 2) {
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k++;
}
else
{
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k--;
}
*(negative_number + 2 * j + 1) = j;
}
if (power / 2 != 0)
if (*(negative_number + power - 1) < 0)
*(negative_number + power - 1) -= 1;
printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));

for (int i = 0; i < power * 2 - 1; i++) {
for (int j = 0; j < power * 2 - 2 * i - 1; j++) {
if (*(negative_number + 2 * j) > *(negative_number + 2 * j + 2))
{
int tmp_mean = *(negative_number + 2 * j);
int tmp_index = *(negative_number + 2 * j + 1);
*(negative_number + 2 * j) = *(negative_number + 2 * j + 2);
*(negative_number + 2 * j + 2) = tmp_mean;
*(negative_number + 2 * j + 1) = *(negative_number + 2 * j +
3);
*(negative_number + 2 * j + 3) = tmp_index;
}
}
}

printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));
/*for(int j=0;j<power;j++){
for(int i=0;i<power;i++){
printf("%d ", *(array + *(negative_number + 2 * j + 1) * power +
i));
}
printf("n");
}
*/
}









share|improve this question
















The problem is that function "sort" doesn`t work propably. It should find the number of negative elements in columns of certain zone(look down)
zone

then it shold sort columns with the number of that negativee elements.
Could you please help me and show my mistakes?



#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

void randInit(int *a, int power);
void array_output(int *array, int power);
double avarage_sum(int *array, int power);
void sort(int *array, int power);


int main() {
srand(time(NULL));
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
printf("Введіть розмір квадратної матриці матриці:n");
int m;
scanf_s("%d", &m);
int *a = (int*)malloc(m*m * sizeof(int));
randInit(a, m);
array_output(a, m);
printf("nОсь середнє значення заданих елементів:
%.2lf",avarage_sum(a,m));
sort(a, m);
_getch();
return 0;
}

void randInit(int *a, int power) {
srand(time(NULL));

for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++) {
*(a + i * power + j) = -20 + rand() % 40;
}
}
}

void array_output(int *array, int power) {
for (int i = 0; i < power; i++) {
for (int j = 0; j < power; j++)
printf("%dt", *(array + i * power + j));
printf("n");
}
}

double avarage_sum(int *array, int power) {
int k = 0;
int n = 0;
double sum = 0.0;
for (int i = 0; i < (power / 2); i++) {
for (int j = i; j < power - k; j++) {
sum += *(array + i * power + j);
n++;
}
k++;
}
k = 0;
for (int i = power - 1; i > (power / 2); i--) {
for (int j = power - k - 1; j > k - 1; j--) {
sum += *(array + i * power + j);
n++;
}
k++;
}
if (power % 2 == 0)
sum;
else {
sum += *(array + (power*(power / 2)) + (power / 2));
n++;
}

return sum / n;
}

void sort(int *array, int power) {
int k = 0;
int *negative_number = (int*)calloc(power * 2, sizeof(int));

for (int j = 0; j < power; j++) {
if (k < power / 2) {
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k++;
}
else
{
for (int i = 0; i <= k; i++) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
for (int i = power - 1; i >= power - k - 1; i--) {
if (*(array + i * power + j) < 0)
*(negative_number + 2 * j) += 1;
}
k--;
}
*(negative_number + 2 * j + 1) = j;
}
if (power / 2 != 0)
if (*(negative_number + power - 1) < 0)
*(negative_number + power - 1) -= 1;
printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));

for (int i = 0; i < power * 2 - 1; i++) {
for (int j = 0; j < power * 2 - 2 * i - 1; j++) {
if (*(negative_number + 2 * j) > *(negative_number + 2 * j + 2))
{
int tmp_mean = *(negative_number + 2 * j);
int tmp_index = *(negative_number + 2 * j + 1);
*(negative_number + 2 * j) = *(negative_number + 2 * j + 2);
*(negative_number + 2 * j + 2) = tmp_mean;
*(negative_number + 2 * j + 1) = *(negative_number + 2 * j +
3);
*(negative_number + 2 * j + 3) = tmp_index;
}
}
}

printf("nОсь відсортований масив:n");
for (int i = 0; i < power * 2; i++)
printf("%d ", *(negative_number + i));
/*for(int j=0;j<power;j++){
for(int i=0;i<power;i++){
printf("%d ", *(array + *(negative_number + 2 * j + 1) * power +
i));
}
printf("n");
}
*/
}






c arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 16:36









Swordfish

1




1










asked Nov 25 '18 at 16:30









Олег ГацькоОлег Гацько

1




1








  • 1





    C tip for the day: you can do cast input pointer into 2d array with int (*array2d)[power] = (void*)array; in your functions and then simply access with like array2d[i][j].

    – Kamil Cuk
    Nov 25 '18 at 16:38














  • 1





    C tip for the day: you can do cast input pointer into 2d array with int (*array2d)[power] = (void*)array; in your functions and then simply access with like array2d[i][j].

    – Kamil Cuk
    Nov 25 '18 at 16:38








1




1





C tip for the day: you can do cast input pointer into 2d array with int (*array2d)[power] = (void*)array; in your functions and then simply access with like array2d[i][j].

– Kamil Cuk
Nov 25 '18 at 16:38





C tip for the day: you can do cast input pointer into 2d array with int (*array2d)[power] = (void*)array; in your functions and then simply access with like array2d[i][j].

– Kamil Cuk
Nov 25 '18 at 16:38












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469542%2ffinding-the-number-of-negative-elements-in-columns-of-certain-zone-and-sorting-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469542%2ffinding-the-number-of-negative-elements-in-columns-of-certain-zone-and-sorting-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen