badly placed ()'s


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting badly placed ()'s
# 1  
Old 08-23-2009
badly placed ()'s

i'm trying to run the following program but i keep getting the message "badly placed ()'s" can u help?
Code:
#include "modularity_mat.h"
#include "../sparse_mlpl/sparse_matrix.h"

adj_matrix_arr* allocate_mem_for_matrix_arr (int y) {
    /* Create the adj matrix and allocate memory */
    adj_matrix_arr* A;
    A = ((adj_matrix_arr*)malloc(sizeof(adj_matrix_arr)));
    if (A == NULL) {
        printf("Memory allocation for matrix failed");
        return NULL;
    }
    /* Initialize values */
    A->size = y;
    A->values = ((elem*)calloc(y*y, sizeof(elem)));
    if (A->values == NULL) {
        printf("Memory allocation for matrix failed");
        return NULL;
    }    
    return A ;}

group_arr* allocate_mem_for_group_arr (int y) {

    /* Create the group and allocate memory */
    group_arr* g;
    g = (malloc(sizeof(group_arr)));
    if (g == NULL) {
        printf("Memory allocation for group failed");
        return NULL;}

    /* Initialize values */
    g->size = y;
    g->values = ((int*)calloc(y, sizeof(int)));
    if (g->values == NULL) {
        printf("Memory allocation for group failed");
        return NULL;}    
    return g ;}

adj_matrix_arr *get_matrix_from_file (char *f_name){
    int y , i;
    elem num;
    adj_matrix_arr *A;    
    FILE *m_file;

    /* Open file handlers */
    m_file = fopen(f_name, "r");
    if ( m_file == NULL ) {
        printf ("file not found");
        fclose(m_file);
        return NULL;
    }

    /* Read data */
    if ((fscanf(m_file, "%d", &y)) <= 0) {
        printf("scan failed");
        fclose(m_file);
        return NULL;
    }
    A = allocate_mem_for_matrix_arr (y);
    for (i=0; i < y*y; i++) {
        if ((fscanf(m_file, "%lf", &num)) <= 0) {
            fclose(m_file);
            printf("Scan failed");
            return NULL;
        }
        A->values[i] = num;
    }
    fclose(m_file);
    return A;
}

group_arr *get_group_from_file(char *f_name) {
    FILE *g_file;
    group_arr* g;
    int num; 
    int n, i;

    /* Open file handlers */
    g_file = fopen(f_name, "r");
    if (g_file == NULL ) {
        printf("file not found");
        fclose(g_file);
        return NULL;
    }

    /* Read data */        
    g = allocate_mem_for_group_arr(0);
    for(i=0 ; i<n; i++){
        if (fscanf(g_file , "%d", &num) <= 0) 
            break;
        g->size++;
        g->values = (int*)realloc(g->values, sizeof(int)*(i+1));
        if (g->values == NULL) {
            printf("Memory allocation for group failed");
            fclose(g_file);
            return NULL;
        } 
        g->values[i]=num ;}
    fclose(g_file);
    return g ;}



/* Given a adj matrix and a group, create the sparse sub-matrix according to the group */
adj_matrix_arr* create_mat(adj_matrix_arr* m, group_arr *g) {
    int i,j;
    int y = g->size;
    adj_matrix_arr* A;
    A = allocate_mem_for_matrix_arr(y);
    if (A == NULL) return NULL;
    for (i=0; i < y; i++) {
        for (j=0; j < y; j++) {
            A->values[i*y + j] = m->values[(g->values[i]) * (m->size) + g->values[j]];
        }
    }
    return A;
}




adj_matrix_arr* edges_count(adj_matrix_arr* m) {
    int i,j;
    int M = 0;
    adj_matrix_arr* A;
    int* deg;
    deg = (int*)calloc(m->size, sizeof(int));
    if (deg == NULL) {
        printf("Memory allocation failed");
        return NULL;
    }
    for (i=0; i<m->size;i++) { 
        for (j=0; j<m->size;j++) {
            deg[i] += (int)m->values[i*m->size + j];
        }
    }
    
    for(i=0; i<m->size; i++)
        M += deg[i];
    
    if ((A = allocate_mem_for_matrix_arr(m->size)) == NULL) return NULL;
    
    for (i=0; i<m->size;i++) { 
        for (j=0; j<m->size;j++) {
            A->values[i*A->size + j] = (deg[i]*deg[j])/(double)M;
        }
    }
    
    return A;
;}


adj_matrix_arr* create_modol_m(adj_matrix_arr* A, adj_matrix_arr* C) {
    int i,j;
    int y = A->size;
    adj_matrix_arr *B;
    B = allocate_mem_for_matrix_arr(y);
    if ( B == NULL) return NULL;
    for (i=0; i < y; i++) { 
        for (j=0; j < y; j++)
            B->values[i*y + j] = A->values[i*y + j] - C->values[i*y + j];
    }
    return B;
}    




elem* calc_fg(adj_matrix_arr *B) {
    int i,j;
    int y = B->size;
    elem *fg;
    fg = calloc(y, sizeof(elem));
    if (fg == NULL) {
        printf("memory allocation failed");
        return NULL;
    }
    for (i=0; i < y; i++)
        for (j=0; j < y; j++){
            fg[i] += B->values[i*y + j];}
    return fg;
}

adj_matrix_arr* calc_g_modol_m(adj_matrix_arr* Bg, elem *fg) {
    int i,j,y;
    adj_matrix_arr *HBj;

    y = Bg->size;
    HBj = allocate_mem_for_matrix_arr(y);
    if (HBj == NULL) return NULL;
    for (i=0; i < y; i++)
        for (j=0; j < y; j++)
            HBj->values[i*y + j] = Bg->values[i*y + j] - delta(i,j)*fg[i];
    return HBj;
}

void print_matrix(adj_matrix_arr* A) {
    int i,j;
    int y = A->size;
    printf("%d \n", y);
    for (i=0; i < y; i++) {
        for (j=0; j < y; j++) {
            printf("%.6f ", A->values[i*y + j]);
            }
        if (i != y-1)
            printf("\n");
    }
}

int delta(int i, int j) {
    if (i == j)
        return 1;
    else 
        return 0;
}

adj_matrix_arr *create_g_mod_m(adj_matrix_arr *A, group_arr *g) {
    adj_matrix_arr *Ag, *B, *Bg, *HBj, *C;
    elem* fg;
    
/* Create the necessary data for eigenpair computation */
    Ag = create_mat(A, g);
    if (Ag == NULL) return NULL;
    C = edges_count(A);
    if (C == NULL) {
        free_adj_matrix_arr(Ag);
        return NULL;
    }
    B = create_modol_m(A, C);
    if (B == NULL) {
        free_adj_matrix_arr(Ag);
        free_adj_matrix_arr(C);
        return NULL;
    }
    Bg = create_mat(B, g);
    if (Bg == NULL) {
        free_adj_matrix_arr(Ag); 
        free_adj_matrix_arr(C);
        free_adj_matrix_arr(B);
        return NULL;
     }
    fg = calc_fg(Bg);
    if (fg == NULL) {
        free_adj_matrix_arr(Ag);
        free_adj_matrix_arr(C);
        free_adj_matrix_arr(B);
        free_adj_matrix_arr(Bg);
        return NULL;
    } 
    HBj = calc_g_modol_m(Bg,fg);
    if (HBj == NULL) {
        free_adj_matrix_arr(Ag);
        free_adj_matrix_arr(C); 
        free_adj_matrix_arr(B);
        free_adj_matrix_arr(Bg);
        free(fg);
        return NULL;
    }
    
    free_adj_matrix_arr(Ag);
    free_adj_matrix_arr(C);
    free_adj_matrix_arr(B);
    free_adj_matrix_arr(Bg);
    free(fg);
    return HBj;
}



void free_adj_matrix_arr(adj_matrix_arr* m) {
    if (m != NULL) {
        free(m->values);
        free(m);
    }
}



void free_group_arr(group_arr* g) {
    if (g != NULL) {
        free(g->values);
        free(g);
    }
}


Last edited by jim mcnamara; 08-23-2009 at 03:30 PM.. Reason: code tags
# 2  
Old 08-23-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 08-23-2009
Hi Roni, "HBj" seemed a variable to me below,if so you should use square brackets [] instead of ().

Code:
    HBj = allocate_mem_for_matrix_arr(y);
    if [HBj == NULL] return NULL;

Besides as far as i know in FOR DO loops you should use double rounded brackets below

Code:
    for ((i=0; i < y; i++))
        for ((j=0; j < y; j++))


Last edited by EAGL€; 08-23-2009 at 06:22 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Badly placed ()'s

Edit - I don't know how to delete posts. The question I asked here ended up not being the question I should have asked as I didn't realise I needed to edit my script to comply with SGE. Hi, My script is: #!/bin/bash # Perform fastqc on files in a specified directory. for ((j=1;... (8 Replies)
Discussion started by: una1992
8 Replies

2. Programming

Badly places ()'s on C

I dont know why this Linux would give me badly placed () error all the time for this; #include <stdio.h> int main() { register int num=0 ; while ((num < 5)) ++num; printf("Pass %d \n", num) ; return 0 ; } can anyone help me please? (11 Replies)
Discussion started by: sizzler786
11 Replies

3. Shell Programming and Scripting

Badly Placed ()'s when trying to use csh

The terminal is bash. Whenever I try to execute csh just by itself it gives Badly Placed ()'s. Whenever I try to use csh with a script it also gives Badly Placed ()'s. My script is this, there is nothing wrong with it, since it used by other students in class as well. (I have also asked... (5 Replies)
Discussion started by: quantumizer
5 Replies

4. Shell Programming and Scripting

Another case of Badly placed parens.

The shell error message "Badly placed ()'s" can occur for a surprisingly simple oversight. If the script begins with a shell-invocation comment, but is missing the exclamation-point, it is simply a comment and not an invocation. If you attempt to execute it from a shell other than the shell you... (4 Replies)
Discussion started by: Dickster
4 Replies

5. UNIX for Dummies Questions & Answers

Need Help Badly: Newbie Here

I have this file: NPANXX|BILLDATE|DIVCODE|COMID|RAO|LIKECODE|BOSS|SORD|STATECODE| 087001|BP01|H|SWBT| |041|IMR6|IMSR6|AR| 087002|BP03|H|SWBT| |042|IMR6|IMSR6|AR| 087003|BP05|H|SWBT| |043|IMR6|IMSR6|AR| .... these are the things that i HAve to do: Insert a new column named “TEST” All... (14 Replies)
Discussion started by: arkhei
14 Replies

6. Shell Programming and Scripting

Badly formed number.

I have the following script running every day numerous times a day and it works fine, but very occasionally I get the following error if: Badly formed number. Anyone know why? Here is the script that runs with the follow parms LCTMDBSE 100000 130000 160000 #!/bin/csh ... (0 Replies)
Discussion started by: Northerner
0 Replies

7. Shell Programming and Scripting

sed help needed badly

how can I delete one line above and below the matching pattern ? e.g I want to delete the line above and below the line with %CLI- in example below : $CHECKSUM $1$DGA1043:TSTST01.DBF;1 %CLI-E-OPENIN, error opening $1$DGA1043:TSTST01.DBF -RMS-E-FLK, file currently locked by another user ... (6 Replies)
Discussion started by: aliyesami
6 Replies

8. Shell Programming and Scripting

for:badly formed number

Hi, I am doing the following but it complains and says "for:badly formed number" does anyone know why? #!/bin/tcsh foreach(....) ............ set depth=64 set width=23 if ($depth==64) then echo "if" set addr_bits=5 else echo "else" endif echo "addr_bits:$addr_bits" echo... (3 Replies)
Discussion started by: ROOZ
3 Replies

9. Shell Programming and Scripting

Badly placed ()'s. - error

Hi, when I execute the below script, I am getting following error "Badly placed ()'s". can anyone please help me fix ---------------------------------------------------------- # Usage: ani -n -a -s -w -d # # # help_ani() To print help # help_ani() { echo "Usage: $0 -n -a -s -w -d"... (3 Replies)
Discussion started by: amitrajvarma
3 Replies

10. Shell Programming and Scripting

New to it all, But i wanna script really badly!!!

Ok, im brand new to this whole thing, well nearly, but all i wanna know and do is scripting, WHAT DO I DO?!:confused: (3 Replies)
Discussion started by: TheNewGuy
3 Replies
Login or Register to Ask a Question