473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Program Help

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[i];
Objects[i].n_link = Temp;
Objects[i].p_link = Temp->p_link;
Temp->p_link = &Objects[i];
Objects[i].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.

Feb 3 '06 #1
2 1579
du******************@yahoo.com wrote:
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.

--
Ian Collins.
Feb 3 '06 #2
On Thu, 02 Feb 2006 18:52:40 -0800, duncanblacksmithmath wrote:
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.

--
Ben.

Feb 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
2
by: stanlo | last post by:
Hallo to everyone, i am just begining to learn c++ even though i did pascal when i studied mathematics in the univesity.i just took on my self a project which writing a c++ program which does...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
1
by: Willing 2 Learn | last post by:
Below is a program I did to recognize a Finite State Automata for ASCII (J+H)*. I got that one working but im having trouble getting the NFA program to work. I really desperately need help! My...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
0
by: ashishbathini | last post by:
Hi guys here is my problem ... this is the source code I Have , honestly I hav no idea how it works bcos its too complicated for me .... but my problem is ... i hav a freq comonent in it .......
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.