GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-May-2009, 02:43
raduq raduq is offline
New Member
 
Join Date: May 2009
Posts: 4
raduq is on a distinguished road
Exclamation

Return type of main is not int


Hello!

I have to resolve a problem for a test and i've stumbled upon an error. The compiler gives me "return type of main is not int", "no newline at end of file" and syntax error at end of input.

The text of the problem goes like this : "Create a function which verifies if there is a path from a node V to a node W inside an oriented graph."

I've done some code... here it is... please someone tell me where i'm doing wrong, and if i am please give me a solution
The compiler i've used is Codeblocks...



CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int nr_noduri,matrice_adiacenta[10][10];

int parcurgere_nod(int nod_curent,int nod_final){
    int i;
    if(nod_curent==nod_final) return 1;
    else{
        for(i=0;i<nr_noduri;i++){
            if(matrice_adiacenta[nod_curent][i]==1)
            {
                printf("Se parcurge nodul %d",i);
                parcurgere_nod(i,nod_final);
        }
    }
    return 0;
}

void main(){
    printf("Please input the number of nodes");
    scanf("%d",&nr_noduri);
    int i,j;
    for(i=0;i<nr_noduri;i++){
        for(j=0;j<nr_noduri;j++){
            printf("Please input the element [%d][%d] of the adjacency matrix\n",i+1,j+1);
            scanf("%d",&matrice_adiacenta[i][j]);
        }
    }
    printf("Input the starting node\n");
    int nod_i;
    scanf("%d",&nod_i);
    printf("Input the arrival node\n");
    int nod_f;
    scanf("%d",&nod_f);
if(parcurgere_nod(nod_i,nod_f)==1) printf("There is a path from node %d to node %d",nod_i,nod_f);
else if(parcurgere_nod(nod_i,nod_f)==0) printf("There isn't a path from node %d to node %d",nod_i,nod_f);
}
Last edited by raduq : 26-May-2009 at 02:48. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 26-May-2009, 06:25
raduq raduq is offline
New Member
 
Join Date: May 2009
Posts: 4
raduq is on a distinguished road

Re: Return type of main is not int


I've also tried another time... rewriting the entire code, thinking i might have missed something...

Here's the second version... but it still won't work...

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int *v[10][10],nr_noduri,q;

void parcurgere_nod(int nod_curent,int nod_final){
    printf("S-a intrat in functie");
    if(nod_curent==nod_final) printf("Exista cale intre cele doua noduri");
    for(q=0;q<nr_noduri;q++){
        if(v[nod_curent][q]==1) parcurgere_nod(q,nod_final);
    }
}

void main(){
    int i,j;
    printf("Introduceti numarul de noduri.\n");
    scanf("%d",&nr_noduri);
    for(i=0;i<nr_noduri;i++){
        for(j=0;j<nr_noduri;j++){
            printf("Introduceti elementul [%d][%d] al matricei de adiacenta.\n",i+1,j+1);
            scanf("%d",&v[i][j]);
        }
    }
    int nod_i,nod_f;
    printf("Introduceti nodul de plecare. ");
    scanf("%d",&nod_i);
    printf("Introduceti nodul de sosire. ");
    scanf("%d",&nod_f);
    parcurgere_nod(nod_i,nod_f);
}
I want to add that upon running the program, when it enters the function, it skips the first "if" and then doesn't even enter the "for" ... why does it do that?
Last edited by admin : 26-May-2009 at 06:29. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #3  
Old 26-May-2009, 08:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Return type of main is not int


Quote:
Originally Posted by raduq
"return type of main is not int"
So, declare main() to have an int return type. The "usual" return value for normal program exit is zero

CPP / C++ / C Code:
int main()
{
.
.
.
    return 0;
}
Quote:
Originally Posted by raduq
, "no newline at end of file" and syntax error at end of input.

So: With the file open in your text editor, go to the point after the last character on the last line. Press 'Enter' to make sure that the last line is terminated by a newline. .The editor that I use will put a newline at the end of the file whether you enter it or not. Apparently yours does not.

With my compiler (GNU gcc) both of these are warnings, not errors, but I recommend that you "fix" all of your program to eliminate warnings like this. It turns out that some warnings may be highly significant, and eliminating unnecessary warnings makes for less confusion and will decrease the likelihood of your overlooking something important in the compiler messages.


Also I note that your function parcurgare_nod() has four left braces '{' but three right braces '}'

Here is what it looks like when I format it with indentation that shows the structure:

CPP / C++ / C Code:
int parcurgere_nod(int nod_curent, int nod_final)
{
    int i;
    if (nod_curent == nod_final)
        return 1;
    else {
        for (i = 0; i < nr_noduri; i++) {
            if (matrice_adiacenta[nod_curent][i] == 1) {
                printf("Se parcurge nodul %d", i);
                parcurgere_nod(i, nod_final);
            }
        }
        return 0;
    }
/* 
 * Note that there is a missing right brace somewhere in the above function.
 * I think you have misplaces some parts of the program in the if-else stuff. 
 */

    void main() {

Regards,

Dave
  #4  
Old 26-May-2009, 08:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Return type of main is not int


Quote:
Originally Posted by raduq
...

Here's the second version... ...
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int *v[10][10],nr_noduri,q;
The variable v is declared as a 2-D array of pointers to int. Is that what you had in mind? Or should it be a 2-D array of ints?


The following statement should have given a warning about comparing a ponter to an int:

CPP / C++ / C Code:
        if (v[nod_curent][q] == 1)

I think you should probably change the declaration of v to
CPP / C++ / C Code:
    int v[10][10];
Regards,

Dave
  #5  
Old 26-May-2009, 09:59
raduq raduq is offline
New Member
 
Join Date: May 2009
Posts: 4
raduq is on a distinguished road

Re: Return type of main is not int


yeah, i mended those errors eventually, but the program still won't work... i've ended up with a final version of it... here it is...

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int v[10][10],nr_noduri,q,nod_i,nod_f,a[10];

void parcurgere_nod(int nod_curent,int nod_final){
    if(nod_curent==nod_final) printf("Exista cale intre cele doua noduri");
    else{
        a[nod_curent]=1;
        for(q=0;q<nr_noduri;q++){
            if(v[nod_curent][q]==1 && a[q]==0) parcurgere_nod(q,nod_final);
        }
    }
}

int main(){
    int i,j;
    printf("Introduceti numarul de noduri.\n");
    scanf("%d",&nr_noduri);
    for(i=0;i<nr_noduri;i++){
        for(j=0;j<nr_noduri;j++){
            printf("Introduceti elementul [%d][%d] al matricei de adiacenta.\n",i+1,j+1);
            scanf("%d",&v[i][j]);
        }
    }
    printf("Introduceti nodul de plecare. ");
    scanf("%d",&nod_i);
    printf("Introduceti nodul de sosire. ");
    scanf("%d",&nod_f);
    parcurgere_nod(nod_i-1,nod_f-1);
    return 0;
}
The program still won't work... i don't know where i'm doing wrong... but it won't work... if anyone could please tell me what i'm doing wrong...

I repeat that the program has to be composed out of a function that verifies if there is a path between two nodes of an directed graph.
Last edited by admin : 27-May-2009 at 01:03. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #6  
Old 26-May-2009, 11:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Return type of main is not int


Quote:
Originally Posted by raduq
y


//The program still won't work...

What doesn't work?

Does it say that a path exists when you have entered a matrix for which no path exists?

Does it fail to find a path for a matrix for which there is a path?

What?

In other words:

1. Show us the input that you gave the program.

2. Tell us exactly what happened. (Show the output.)

3. Tell us what you expected to happen.

4. Tell us exactly what you don't understand about the difference between 2 and 3.


Regards,

Dave
  #7  
Old 26-May-2009, 12:41
raduq raduq is offline
New Member
 
Join Date: May 2009
Posts: 4
raduq is on a distinguished road
Red face

Re: Return type of main is not int


I found where my mistake was... i had to change the "q" variable from global to local, in the function...

The input was :
0 1 0 1
0 0 0 0
0 0 0 0
0 0 1 0

Starting node: 1
Final node: 3

And there was no other output...coresponding to there being no path between the nodes

I expected the function to verify each line of the matrix and where it found a 1(representing the nodes that are connected - by intersection of line and column in the matrix),to recall the function starting from that node.
For example:
On my first line: 0 1 0 1 ... it found the first one then i would have expected to recall the function with the nodes 2 and the final one ( parcurgere_nod(2,final_node) - and go to the second line of the matrix and search for another 1 in the line then call the function for that node with which it is connected and so on ), then go to the other 1 in the first line of the matrix which coresponds to the 4th node and call the function for node 4 and the final node( parcurgere_nod(4,final_node) - and if it found another 1 in the fourth line of the matrix it would go to the node with which is connected and so on until... hopefully it would find a path to the final node.

I expected the function to call itself over and over until the node that it would have called at one time would be the same with the final node...so, proving that there is a path between the initial node and the final node.

In my example i expected it to go like this... i will put an X where i expected the function to be.

On first call... it would check the first line and find the first 1, which is replaced by the X.
0 X 0 1
0 0 0 0
0 0 0 0
0 0 1 0
Then the program would go to the next 1 of the first line
0 1 0 X
0 0 0 0
0 0 0 0
0 0 1 0
and for the places where it found the 1, replaced by X's it would call the function for that node.
At the first 1 in the first line, the function would check the second line of the matrix for ones...and fine none... At the second 1 in the first line, the function would check the fourth line of the matrix for ones... and find one 1, which coresponds to the third node, and thus going to the third line... where upon calling the function, the "if" condition would be true and the message would appear.

I hope I was clear enough what I expected the program to run like...
  #8  
Old 26-May-2009, 13:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Return type of main is not int


Quote:
Originally Posted by raduq
I found where my mistake was...
So: now does it work to your satisfaction?
Quote:
Originally Posted by raduq
I hope I was clear enough
In general, I would say that if people give more information in their original posts it might take fewer iterations to get meaningful help. I mean "it doesn't work" really doesn't give us enough to work with if we want to try to help you debug the thing.

However...

If you, yourself, have found and fixed the problem, that's probably better anyhow.



Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Web Server newbie problem niss3 Apache Web Server Forum 1 13-Apr-2009 19:38
Torrents Download Problem chandeep Computer Software Forum - Linux 7 09-Oct-2006 23:37
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.