-
Help me please :-)
Ok, basically this program is supposed to put 25 random numbers into a link list, then put them in order, print them out in order, then print the sum and average, it allows me to compile it, but when i run it, it says "memory fault (coredump)"
Here is the code:
Code:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct xnode
{
int num;
struct xnode *next;
};
struct xnode *hptr;
struct xnode *cptr;
void addn();
void order();
void display();
void del();
void main()
{
float ave;
int i, x, sum;
srand(time(NULL));
hptr->num = rand() % 99 + 1;
hptr->next = NULL;
for (i = 0; i <= 25; i++)
{
x = rand() % 99 + 1;
addn(x);
}
while(cptr->next != NULL)
{
cptr = hptr;
sum += cptr->num;
cptr = cptr->next;
}
ave = sum / 25;
printf("The sum of all numbers is %d\n", sum);
printf("The average number is %.2f\n", ave);
order();
display();
del();
}
void addn(int x)
{
struct xnode *nodeptr;
nodeptr = malloc(sizeof(struct xnode));
nodeptr->num = x;
nodeptr->next = NULL;
cptr->next = nodeptr;
}
void order()
{
int temp;
while (cptr->next !=NULL)
{
if (cptr->num > hptr->num)
{
temp = hptr->num;
hptr->num = cptr->num;
cptr->num = temp;
}
cptr = cptr->next;
}
}
void display()
{
int e;
while(cptr!=NULL)
{
printf("Node %d = %d\n", e, cptr->num);
cptr = cptr->next;
e++;
}
}
void del()
{
struct xnode *dptr;
while(cptr != NULL)
{
dptr = cptr->next;
free(cptr);
cptr=dptr;
}
}
Thanks in advance
-
Use some printf()'s to figure where the error happens. What way there might be an issue with the malloc, or free, or some other access to memory causes an error. Also, your addn() function prototype does not declare that it will receive any variables, you need to change the prototype so it will.
-
hptr pointer in main() is NULL, memory needs to be allocated before using.
hptr->num = rand() % 99 + 1;
-
thanks, well i did that
Code:
printf("0000\n");
hptr->num = rand() % 99 + 1;
hptr->next = NULL;
printf("111111\n");
and it didnt get to the line 111111
so what am i doing wrong in those 2 lines?
-
Did you do what scarlet said, i missed that with the quick overview i did. He is right, you try to assign the member num, and the member next of a struct that hptr points to, but there is no struct, no memory has been allocated yet for that. You need to allocate atleast 1, or declare atleast a biginning struct in the code before you can assign it values. Do something like this right before that code.
Code:
hptr = malloc(sizeof(struxt xnode));
-
-
Also, cptr pointer used in addn() will be NULL when first used, memory needs to be allocated before using.
cptr->next = nodeptr;
-
mkay, i fixed those, but my display function doesnt work at all :-( anyone know why thanks for all your help guys/girls
-
And there's more! The following changes will get the numbers displayed. Your mainly not setting cptr back to hptr before using, and using while(cptr->Next) in loops which should be just cptr. In the first while loop you were going cptr = hptr within the loop which should be outside.
Code:
void main()
{
...
cptr = hptr = (struct xnode*)malloc(sizeof(struct xnode));
...
...
cptr = hptr;
while(cptr != NULL)
{
sum += cptr->num;
cptr = cptr->next;
}
...
}
void addn(int x)
{
...
cptr->next = nodeptr;
cptr = cptr->next;
cptr->next = NULL;
}
void order()
{
...
cptr = hptr;
while (cptr !=NULL)
{
...
}
}
void display()
{
...
cptr = hptr;
while(cptr!=NULL)
{
...
}
}
void del()
{
cptr = hptr;
struct xnode *dptr;
...
}
-
:-D you kick ass!! thanks alot
-
ok, another problem, lol
this function is supposed to put the link list in order but it doesnt work
Code:
void order()
{
int temp;
cptr = hptr;
while (cptr->next !=NULL)
{
if (cptr->num > hptr->num)
{
temp = hptr->num;
hptr->num = cptr->num;
cptr->num = temp;
}
cptr = cptr->next;
}
}
again thanks alot for all your help
-
You need to keep loopng through the list until there's no more swaps, you were just going once. Also you need the compare with the next number in the list, then swap with the next.
Code:
void order()
{
int temp;
bool bSorted = 0;
while(!bSorted)
{
bSorted = 1;
cptr = hptr;
while (cptr !=NULL)
{
if( cptr->next && cptr->num > cptr->next->num)
{
temp = cptr->num;
cptr->num = cptr->next->num;
cptr->next->num = temp;
bSorted = 0;
}
cptr = cptr->next;
}
}
}
-
thanks, question, what is bool?
-
A bool is a boolean value which can only be TRUE or FALSE. You could have done the same with an int
int iSorted = 0;