Program Help

  • Thread starter duncanblacksmithmath
  • Start date
D

duncanblacksmithmath

I know a lot of you have seen this before but I have worked on the
program and have gotten it to work thus far but I need help getting
these two functions to work and implementing them.
Here is what I have been trying to work out for these functions but
can't get them to fully work out for me.

information:
A function to access an element of a list by its index called access
i(LL, i). It traverses
the list LL and returns the address of the i-th element in LL, if there
is one. Otherwise it
returns null.

A function to access an element of a list by value called access v(LL,
V). This function
traverses the list LL searching for an element with the value V. The
address of the rst such
element, if found, is returned. Otherwise a null pointer is returned.

What is needed in the main:
Access the 10-th identifer and the name "XYZ" in the list NAMES. Print
the identifer hold
on the 10-th place in the list NAMES and also print the place (index
and/or address) of the
list element where your program found the name "XYZ". Provide for all
contingencies.

functions (outline):

struct Val *access_i(struct Obj *l, int i)
{
struct Obj *next=l;
int j=0;
while (j != i)
{
if (next -> n_link == NULL)
return (NULL);
else
{
next=next->n_link;
j=j+1;
}
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);
}

struct Val *access_v(struct Obj *l, int v)
{
struct Obj *next=l->n_link;
int val=next->p_value;
while (val != v)
{
if (next -> n_link == NULL)
return (NULL);
else
{
next=next->n_link;
val=next->p_value;
}
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);
}

errors:
program1.c: In function `access_i':
program1.c:72: warning: return from incompatible pointer type
program1.c: In function `access_v':
program1.c:78: warning: initialization makes integer from pointer
without a cast
program1.c:86: warning: assignment makes integer from pointer without a
cast
program1.c:92: warning: return from incompatible pointer type

complete code (compiles and executes correctly):
scan.h reads in a file and in the main the catch functions finds the
numbers, separators, unknowns, and names
/* Compiling command: cc scan.c lms1.c
where lms1.c is this file;
Calling sequence: "a.out Filename"
where Filename is the name of a text file */

#include <stdio.h>
#include <stdlib.h>
#include "scan.h"

struct Val
{
char type;
int length;
char value[256];
} Values[1000];

struct Obj
{
struct Obj *p_link;
struct Val *p_value;
struct Obj *n_link;
} Objects[1000], *IdList, *NrList, *SpList, *UnList ;

int i = 0, j = 0; /* 0 <= i,j <= 999, objects and vcalues indices */

struct Obj *List (struct Obj *h, struct Obj *t)
{
h->p_link = NULL;
h->n_link = t;
h->p_value = NULL;
t->p_link = h;
t->n_link = NULL;
t->p_value = NULL;
return h;
}

int Append(struct Obj *L, struct Val *item)
{
struct Obj *Temp = L->n_link;
while (Temp->n_link != NULL)
Temp = Temp->n_link;
if ((i <= 999))
{
(Temp->p_link)->n_link = &Objects;
Objects.n_link = Temp;
Objects.p_link = Temp->p_link;
Temp->p_link = &Objects;
Objects.p_value = &Values[j];
i = i+1;
return 1;
}
else return 0;
}

int PrintLists(struct Obj *list)
{
struct Obj *Temp = list->n_link;
printf("Type\tLength\tValue\n");
while (Temp->n_link != NULL)
{
printf("%c\t%d\t%s\n", Temp->p_value->type,
Temp->p_value->length, Temp->p_value->value);
Temp = Temp->n_link;
}
}


main (argc, argv)
int argc;
char *argv[];
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int Done = 0;
IdList = List(&Objects[0], &Objects[1]);
NrList = List(&Objects[2], &Objects[3]);
SpList = List(&Objects[4], &Objects[5]);
UnList = List(&Objects[6], &Objects[7]);
i = 8; j = 0;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
printf("Symbol: Identifier %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'I';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (IdList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'N':
{
/* process integer number */
printf("Symbol: Integer number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'N';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'F':
{
/* process real number */
printf("Symbol: Real number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'F';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'W':
{
printf("White symbol received\n");
break;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
else
printf("Unprintable character
discovered\n");
break;
}
case 'O':
{
printf("Symbol: Separator %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'S';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (SpList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'E':
{
printf("Error condition: %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'E';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (UnList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
}
} /* end while */
printf("List of NAMES\n");
PrintLists(IdList);
printf("List of NUMBERS\n");
PrintLists(NrList);
printf("List of SEPARATORS\n");
PrintLists(SpList);
printf("List of UNKNOWNS\n");
PrintLists(UnList);
}

Thanks for the help.
 
I

Ian Collins

errors:
program1.c: In function `access_i':
program1.c:72: warning: return from incompatible pointer type

You declare access_i returning struct Val*, but return a struct Obj*.
program1.c: In function `access_v':
program1.c:78: warning: initialization makes integer from pointer
without a cast
You are assigning a Val* to an int.
program1.c:86: warning: assignment makes integer from pointer without a
cast

As above.
program1.c:92: warning: return from incompatible pointer type
As line 72.
 
B

Ben Bacarisse

functions (outline):

May I offer some advice about program logic?
struct Val *access_i(struct Obj *l, int i)
{
struct Obj *next=l;
int j=0;
while (j != i)

Whenever you write "while (C)" stop and ask "does the loop body always
always work towards making C false?" This is, in theory, very hard but in
most practical cases it is easy! If you get the answer "no" (as you would
in this case) decide if you care about this. It is legitimate, sometimes,
not to care, but it is good to know you are doing so.
{
if (next -> n_link == NULL)

Again, train yourself to stop every time you write "*ptr", "ptr->..." or
"ptr[..]" and look back in the code to see if you can be *sure* that ptr
is valid (in particular not NULL). If you can't be sure (and you can't in
this case) it often means that you have a logical error in what you are
doing.
return (NULL);
else
{
next=next->n_link;
j=j+1;
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);

Apply the same advice here and you will be prompted to think about what
this last "if" is for and why it might go wrong. I don't want to correct
the code because this looks like an assignment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top