0

I'm looking to create a program that creates and stores N random complex numbers. However, it's necessary to use another array (of pointers) which points to every element on the complex array. Then, I have to present it sorted by its norm to the user.

To "sort" it, I just change to where the pointer array is pointing to. Then I just show the "sorted" array of pointers.

But I'm not getting it right. How can I achieve this?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define FORMATLOG "FORMATLOG: invalid parameters: ex3 <N>"
#define RANGE 18 - 6

enum { true, false };

typedef struct {
    double real,
           imag,
           norm;
} Complex;

void generateComplex(int N) {

    int i, test;
    Complex vector[N];

    for(i = 0; i < N; i++) {
        vector[i].real = rand() % RANGE;
        do { vector[i].imag = rand() % RANGE; } while(vector[i].imag == 0);
        vector[i].norm = sqrt(pow(vector[i].real, 2) + pow(vector[i].imag, 2));
    }

    Complex *p_vect = &vector;

    /*  makes array point in order */
    while(test == false) {
        test == true;
        for(i = 0; i < N - 1; i++)
            if(vector[i].norm > vector[i + 1].norm) {
                *p_vect[i] = &vector[i + 1];
                *p_vect[i + 1] = &vector[i];
                test = false;
            }
    }

    for(i = 0; i < N; i++)
        printf("\t%d -| %2g  + %2gi | = %g\n", i, vector[i].real,
                                               vector[i].imag,
                                               vector[i].norm);
    printf("********************************\n");
    for(i = 0; i < N; i++)
        printf("\t%d -| %2g  + %2gi | = %g\n", i, p_vect[i].real,
                                               p_vect[i].imag,
                                               p_vect[i].norm);

}

int main(int argc, char **argv) {

    if(argc != 2) {
        puts(FORMATLOG);
        return false;
    }

    srand(time(NULL));

    int i, N = atoi(argv[1]);
    generateComplex(N);

    return true;
}
5
  • 1
    #define RANGE 18 - 6 --> #define RANGE (18 - 6) Commented Nov 23, 2014 at 16:22
  • 1
    while(test == false) you have not initialised test Commented Nov 23, 2014 at 16:24
  • 2
    enum { true, false } is very confusing. Why do you define true as 0 and false as 1? Commented Nov 23, 2014 at 16:24
  • 2
    For sorting, you could use qsort from <stdlib.h>. Commented Nov 23, 2014 at 16:25
  • Minor: replace sqrt(pow(vector[i].real, 2) + pow(vector[i].imag, 2)) with hypot(vector[i].real, vector[i].imag) Commented Nov 24, 2014 at 3:55

1 Answer 1

2
Complex *p_vect = &vector;

This creates a pointer to the existing array. The requirement is to create an array of pointers, not a pointer to an array.

Complex *p_vect[N];

for (i = 0; i < N; i++) {
    p_vect[i] = &vector[i];
}

If you start with this, you can then sort p_vect without touching vector. Your sorting code, when written correctly, will make no mention of vector at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, because (in your code) *p_vect is essentially an alias for vector, these lines: *p_vect[i] = &vector[i + 1]; *p_vect[i + 1] = &vector[i]; Are just overwriting element i with i+1. The second line is having no effect. You need to use a third variable to swap values.
Oh, I see. Thank you. But then I'm having problems with sorting the array. I'm doing: /* makes array pointer in order */ while(test == false) { test == true; for(i = 0; i < N - 1; i++) if(p_vect[i]->norm > p_vect[i + 1]->norm) { Complex *temp = p_vect[i]; p_vect[i] = p_vect[i + 1]; p_vect[i + 1] = temp; test = false; } } But the pointer array just doesn't sort.
I'll give you a hint. There's a typo with the test variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.