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 17-May-2004, 06:12
nick4 nick4 is offline
New Member
 
Join Date: May 2004
Posts: 4
nick4 is on a distinguished road
Question

help on linked lists any1?????


hi i am a bit of an ammature and would appreciate any help on this. i am tryin 2 write a program that displays the graph of the current wave in the linked list and the sum of all the waves in the list - it is this that does not work. viewing the current wave works fine. i have a feeling it is something simple that is wrong to do with pointers??? here is the code-

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <windows.h>
#include <math.h>
#include "n:/course/elec/include/gfxlib3.h"
typedef struct wave_type // declare structure for WAVE database
{
int number;
unsigned long amplitude;
unsigned long phase;
unsigned long frequency;
unsigned long y_val[550];
struct wave_type *next;
struct wave_type *former;
struct wave_type *previous;
struct wave_type *current;


} WAVE;

void print_wave(WAVE *item) // function to print current WAVE details
{ // to screen
int i;
if (item==NULL)
{
gotoxy(15,1); printf(" End of list ");
Sleep(100); 
}

else
{
if (item != NULL,i=1,i++)
{
gotoxy(15,5); printf(" Waves in list ");
gotoxy(15,9); printf("Wave number: ");
gotoxy(15,11); printf("Amplitude: ");
gotoxy(15,13); printf("Phase(deg): ");
gotoxy(15,15); printf("Frequency(Hz): ");
gotoxy(34,9); printf("%d", item->number);
gotoxy(34,11); printf("%ld", item->amplitude);
gotoxy(34,13); printf("%ld", item->phase);
gotoxy(34,15); printf("%ld", item->frequency);

}

}
}
WAVE * add_wave(WAVE *item) // add a WAVE to the end of the linked list
{
WAVE *new_wave;
double w, freq, phase, y_val[550];
int t, amp, phas; 
printf(" Adding a wave ");

new_wave = (WAVE *) malloc(sizeof(WAVE));
new_wave->next = NULL;
new_wave->previous = item; 

gotoxy(15,9); printf("Wave Number: ");
gotoxy(15,11); printf("Amplitude: ");
gotoxy(15,13); printf("Phase(deg): ");
gotoxy(15,15); printf("Frequency(Hz): ");
gotoxy(34,9); scanf("%ld", &new_wave-> number);
gotoxy(34,11); scanf("%ld", &new_wave-> amplitude);
gotoxy(34,13); scanf("%ld", &new_wave-> phase);
gotoxy(34,15); scanf("%ld", &new_wave-> frequency);

freq = new_wave->frequency;
phase = new_wave->phase;
amp = new_wave->amplitude;
w = 2 * 3.142 * freq;
for (t=1; t < 550; t++)
{

new_wave->y_val[t] = amp * sin(w * t + phase);

} 
if (item != NULL)
{
item->next = new_wave;
}
return (new_wave);
}

WAVE * browse(WAVE *item) // move to next item in list
{
if (item != NULL)
{
if (item->next != NULL)
{
item = item->next;
}
} 
else{
gotoxy(5,5);
printf("This is the last item in the list");
Sleep(1000);
}
return (item);
}


WAVE * browseback(WAVE *item) // move to previous item in list
{
if (item != NULL)
{
if (item->previous != NULL)
{
item = item->previous;
}
}
else{
gotoxy(5,5);
printf("This is the first item in the list");
Sleep(1000);
}
return (item);
}

WAVE * delete_wave(WAVE *item,WAVE **first,WAVE **last ) // delete item from list
{
WAVE *next, *previous;
if(item!=NULL)
{
next=item->next;
previous=item->previous;
if(previous==NULL)
{ 
*first=next;
}
else 
{
previous->next=next;
}
if(next==NULL)
{
*last=previous;
}
else 
{
next->previous=previous;
}

gotoxy(8,5);
printf("This current wave has been deleted from the list");
Sleep(1000);
gotoxy(8,5);
printf(" ");
}
return (item);
}


WAVE * graphic(WAVE *item, WAVE **first, WAVE **last) 
{
double w, freq, amp, phase, y_valu[550], y[550];
int i, t, j, y_value[550], y_v[550];
WAVE *next, *previous, *current;
freq = item->frequency;
phase = item->phase;
amp = item->amplitude;
w = 2 * 3.142 * freq;
for (t=1; t < 550; t++)
{

y_valu[t] = amp * sin(w * t + phase);

} 
gotoxy(15,16);
graphics_on();
setfillstyle(1, WHITE); //make background white
floodfill(0, 0, WHITE);
setcolor(0);
moveto(50,50); //Draw axis
lineto(50,450);
moveto(50,250);
lineto(600,250);

setcolor(4);
moveto(50,250);
for(j=1;j<550;j++)
{
y_value[j]=floor(y_valu[j]); //round values down so they can be plotted on the graph
lineto(j+49,250-y_value[j]); //draw the current wave in the list

} 

do
{ 
y[1]=0;
setcolor(1);
moveto(50,250);

for(i=1;i<550;i++)
{ 
item=first; // I THINK IT IS HERE WHERE IT GOES WRONG
do
{ y[i] = y[i] + item->y_val[i];
item = item->next;
}
while(item!=NULL);
y_v[i] = floor(y[i]);
lineto(i+49,250-y_v[i]);
}
} 
while (!(kbhit()));
graphics_off();
return (item);
}


int main (void) // main function / menu
{
WAVE *first=NULL, *current=NULL, *last=NULL, *previous=NULL;
char response;

system("cls");
do 
{

print_wave(current); // print current item in list to screen

gotoxy(15,19); printf("'a': add a wave");
gotoxy(15,25); printf("'v': view graphical output");
gotoxy(15,20); printf("'d': delete current wave");
gotoxy(15,21); printf("'w': brouwse the list forwards");
gotoxy(15,23); printf("'r': return to start of list");
gotoxy(15,24); printf("'q': quit the list");
gotoxy(15,22); printf("'e': brouwse backwards in the list");

gotoxy(40,29); putch(' '); // overwrite old command
gotoxy(25,29);
printf("Enter command: "); // get new command
response = getch();
gotoxy(34,40); putch(response); // print new command to screen
switch (response) // call relevent function
{
case 'd': current = delete_wave(current, &first, &last); break;
case 'a': last = add_wave(last); break;
case 'w': current = browse(current); break;
case 'v': current = graphic(current, &first, &last); break;
case 'r': current = first; break;
case 'e': current = browseback(current); break;
}

if (first == NULL) current = first = last; 

} while (response != 'q');
system("cls");
return 0;
}
Last edited by dsmith : 17-May-2004 at 09:05. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 17-May-2004, 09:32
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Nick. Welcome to GIDForums

It appears that you and Moltarim are taking a similar class. I gave him the basic layout for a linked list structure exactly like this in this thread. It sounds like your list is working for the most part which is great, but you may want to look this over anyway for a more efficient solution.

Your specific problem you have identified as this:
CPP / C++ / C Code:
y[1]=0;
setcolor(1);
moveto(50,250);

for(i=1;i<550;i++)
{ 
item=first; // I THINK IT IS HERE WHERE IT GOES WRONG
do
{ y[i] = y[i] + item->y_val[i];
item = item->next;
}
while(item!=NULL);
y_v[i] = floor(y[i]);
lineto(i+49,250-y_v[i]);
}
} 
while (!(kbhit()));
graphics_off();
return (item);

The first thing that stands out is that you are initializine y[1] to be zero which is good, but you are not initializing the other 549 items. (Also keep in mind that you are y[1] is the second element in this array as arrays in C are 0 based). These could be anything originally. And then in your loop you have this line:
CPP / C++ / C Code:
y[i] = y[i] + item->y_val[i];
Which is adding the initial undefined value of y[i] to item->y_val[i] and storing it back in y[i].

Also, why is y[i] an array at all? This is the only place that I see it used in your function and you graph it in the same for loop, so I am not sure why you are worried about storing all of these values as you never use them again?

Also, why return anything in this function? As far as I can tell this is a procedure and should just be defined as void.

For the most part, I think you just need to go through your code and simplify it a bit. Ask yourself the same types of questions that I have put here.

If you have more specific questions, feel free to post back here.

HTH,
d
 
 

Recent GIDBlogFirst week of IA training 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
Merge sort on a linked list Temujin_12 CPP / C++ Forum 1 06-Mar-2008 20:33
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 11:32
[include] list1.h -- Linked list class dsmith C Programming Language 2 04-May-2004 09:42
Linked lists vortz83 CPP / C++ Forum 3 20-Mar-2004 06:56

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

All times are GMT -6. The time now is 00:12.


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