program with structs



<code>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <new>

//------------------------------------using----------------------------------//
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
//------------------------------------struct---------------------------------//
struct Node2 {
int _data ;
struct Node * _left;
struct Node2 * _down ;
} ;

struct Node {
int _data ;
struct Node * _next ;
} ;

//------------------------------------prototype------------------------------//
struct Node2 *build();
bool prime(int num);
int divisable_by(int num);
bool check_in_node2(struct Node2 * &head,int search);
bool check_in_node1(struct Node *head,int search);
void put_in_node(struct Node* &head,int num); //problem with function
void put_in_node2(struct Node2 *&head ,int num);
void check_allocated(const struct Node*head);
void check_allocated2(const struct Node2*head);
void print_list( struct Node2 ** head);
void free_list(struct Node * head);
void free_list2(struct Node2 * head);


//------------------------------------main-----------------------------------//
int main()
{
struct Node2 *head=build();
print_list(&head);
free_list2(head);
}
//----------------------------------function---------------------------------//
struct Node2 * build()
{
struct Node2 *head =NULL;
struct Node2 *temp=head;
int num,div;

cin>>num;
while(num!=-1)
{
if(head==NULL) //happens only first time
{
if(prime(num)) //problem if first number is prime
{
head=new(std::nothrow) struct Node2;
head->_data=num;
head->_down=NULL;
head->_left=new(std::nothrow)struct Node;
(head->_left)->_data=num;
}
else //not prime number
{
div=divisable_by(num);
head=new(std::nothrow) struct Node2;
head->_data=div;
head->_down=NULL;
head->_left=new(std::nothrow)struct Node;
(head->_left)->_data=num;
}
}
else //after the first number is entered
{
temp=head;
if(prime(num)) //if it is prime
{
if(check_in_node2(temp,num)) //number is already in node2
{
if(!check_in_node1(temp-> _left,num))
put_in_node(temp->_left,num);
}

else //when it is not in node 2 i want to put it in
{
temp=head;
put_in_node2(head,num);
while(temp->_data!=num)
{
temp=temp->_down;
}
put_in_node(temp->_left,num); //current node2
}
}
else //if it is not prime
{
div=divisable_by(num);

if(check_in_node2(temp,div))
{
if(!check_in_node1(temp->_left,num))
put_in_node(temp->_left,num);
}
else //when i want to enter into node2
{

put_in_node2(head,div);
put_in_node(temp->_left,num); //current node2

}
}

}
cin >> num;
}
return head;
}
//----------------------------------function---------------------------------//
bool prime(int num) //returns true if num is prime
{
for (int i=2;i<num;i++) //going from 2 until an number before num
{
if(num%i==0) //checking if any number divides it
return false; //if it does its not prime so we return false
}
return true; //if it got here no num divided it so its prime
}
//----------------------------------function---------------------------------//
int divisable_by(int num)
{
for(int i=2; i<num;i++) //going from two till a value before num
{
if(prime(i)) //first checking if that num is prime
{ //if it is we want to check if its a divisor of num
if(num%i==0) //checking if it divides it
return i; //if it does we return i
}
//we will always find an i bec. a none prime number is always
//divisable by prime numbers
}
}
//----------------------------------function---------------------------------//
bool check_in_node2(struct Node2 *&temp,int search)
//checking if the number is already in the node2's
{
while(temp!=NULL) //going through boxes
{
if(temp -> _data==search) //if value found in curr box
return true;
else
temp=temp->_down; //advance to next box
}
return false; //if we got here it was'nt found so we return false
//this function also changes the value of temp with & bec, i want to
//know at which node2 the value was found
}
//----------------------------------function---------------------------------//
bool check_in_node1(struct Node *head,int search)
//func checks if a certain value is already in the line of node's
{

while(head!=NULL) //going through box
{
if(head-> _data ==search) //if value found in curr box
return true;

head=head-> _next; //advance to next box
}
return false;
}
//----------------------------------function---------------------------------//
void put_in_node(struct Node *&head, int num) //problem here
{
struct Node *temp=new(std::nothrow) struct Node;
check_allocated(temp); //checking if allocated correctly
temp-> _data = num; //problem is around here
temp -> _next = head;
head=temp;
//free_list(temp);
}
//----------------------------------function---------------------------------//
void put_in_node2(struct Node2 *&head,int num)
//func creates new box of type node2 and enter's it sorted
{
Node2 *temp,*front,*rear;


if(num<head->_data)
{
temp=new(std :: nothrow) struct Node2;
check_allocated2(temp);
temp->_data=num;
temp->_down=head;
head=temp;
}
else
{
rear=head;
front=rear->_down;
while(front!=NULL &&front ->_data<num)
{
rear=front;
front=front->_down;
}
temp=new(std :: nothrow) struct Node2;
check_allocated2(temp);
temp->_data=num;
temp->_down=front;
rear->_down=temp;
}

}
//----------------------------------function---------------------------------//
void check_allocated(const struct Node * head) //same as func below,
//but for type node
{
if(head==NULL)
{
cerr <<"Cannot allocate memory"<<endl;
exit(EXIT_FAILURE);
}

}
//----------------------------------function---------------------------------//
void check_allocated2(const struct Node2 * head) //check if allocated correctly
{
if(head==NULL) //if its empty, it did'nt allocate memory correctly
{
cerr <<"Cannot allocate memory"<<endl; //so we send message
exit(EXIT_FAILURE); //and exit program with failure
}
}
//----------------------------------function---------------------------------//
void print_list( struct Node2 ** head)
{
while(*head!=NULL)
{
cout<<(*head)->_data<<" ";
struct Node * temp=(*head)->_left;
while(temp!=NULL)
{
cout<<temp->_data<<" ";
temp=temp->_next;
}
cout<<endl;
head=&((*head)->_down);
}
}
//----------------------------------function---------------------------------//
void free_list2(struct Node2 * head) //deletes allocated type node2
{
struct Node2 * temp; //helper struct
while(head!=NULL) //until we get to the end
{
free_list(head->_left); //send a line of type node to be freed
temp=head; //giving address
head=head->_down; //advancing to next node2
delete temp; //deleting the last one
}

}
//----------------------------------function---------------------------------//
void free_list(struct Node * head) //deletes allocated node's
{
struct Node * temp; //helper struct

while(head!=NULL) //until the end of chain
{
temp=head; //giving address
head=head->_next; //going to next box
delete temp; //deleting the previous
}
}


<code>
hi, I am having a problem with my program. it is a little comlicated so I will try and explain the best I can.
the program is supposed to get numbers and enter them into a chain of structs.
the struct of type Node2 is supposed to hold only prime numbers and the nodes are the other numbers.
for example if I enter 4,5,6
then the program does as follows
it first check if 4 is prime, because its not It finds the smallest prime number that is a divisor of 4 (which is 2) so the program creates a new node2 and enters the vale 2 as its data, now the box to its 'left' will get the value 4.
next it will get the number 5, because 5 is prime it will create a new box of node2 and enter 5 and then create a box to its left and enter 5.
next it will check 6, 6 is not prime so it will check the smallest prime divisor which is 2, because 2 already is a value for one of the node2 boxes then it will not create a new box, but it will go to the line of node2 and create a new box of type node and enter the value 6.
the problem with my program is that when I enter a prime number in the beginning it causes issues.


I'm adding this here because the code was too long to post, also I was not able to put the code in code format, there was an error when I tried to do it
also I was not able to put the code in code format, there was an error when I tried to do it


You need to use brackets [] not <> and the closing tag looks like [/code] the beginning tag is the same without the '/'.

Your Node structure should probably be defined before your Node2 structure. Also using similar names for these two structures will probably get confusing.

Last edited on
it said error content too long
can't anyone help??
the problem with my program is that when I enter a prime number in the beginning it causes issues.


What's the issue?
It's kind of hard to explain.
First off did you understand what the program is supposed to do?
It's kind of hard to explain.

You wrote the program. If you can't explain what's wrong, you're not likely to succeed fixing it.

Try to provide a canonical bug report for your own program. Three steps:
1. List in detail steps required to reproduce the issue
2. Describe the correct behavior (what should have happened)
3. Describe the incorrect behavior (what actually happened)

The goal here isn't to actually diagnose the cause, but just to identify the symptoms. It's almost impossible to solve a problem that you can't identify, so do that first.
ok, put in an input of 7 then 6(or any input starting with a prime number)
the program gets terminated and I cannot seem to figure out why.
the expected output for the input 7 6 is supposed to be:
7 7
2 6
explanation of output:
because 7 is prime we check if there is a box of type node2 with the value 7,
because there isn't we create one.
then on that same line of node2 we check if there is a node with the value 7,
because there isn't we create one.
then the number 6 isn't prime so we find its smaller prime divisor which is 2,
we check if there is a node2 with the value 2
because there isn't we create one
then on the same line of the node2 with the value 2 we check if there is a node with the value 6,
because there isn't we create one.
I believe my pronlem might be in the function put_in_node
but I'm not 100% sure.
thanx for the help please let me know if something isn't clear or if you have any questions

p.s to end output enter -1
p.p.s
an example of an input that does work in the program is
input:4 7 5 2 -1
output: 2 2 4
5 5
7 7

link to the code:
http://cpp.sh/9jt2b
Last edited on
I break Code in two post, to solve error posting long contents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <new>

//------------------------------------using----------------------------------//
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
//------------------------------------struct---------------------------------//
struct Node2 {
    int _data ;
    struct Node * _left;
    struct Node2 * _down ;
} ;

struct Node {
    int _data ;
    struct Node * _next ;
} ;

//------------------------------------prototype------------------------------//
struct Node2 *build();
bool prime(int num);
int divisable_by(int num);
bool check_in_node2(struct Node2 * &head,int search);
bool check_in_node1(struct Node *head,int search);
void put_in_node(struct Node* &head,int num); //problem with function
void put_in_node2(struct Node2 *&head ,int num);
void check_allocated(const struct Node*head);
void check_allocated2(const struct Node2*head);
void print_list( struct Node2 ** head);
void free_list(struct Node * head);
void free_list2(struct Node2 * head);


//------------------------------------main-----------------------------------//
int main()
{
    struct Node2 *head=build();
    print_list(&head);
    free_list2(head);
}
//----------------------------------function---------------------------------//
struct Node2 * build()
{
    struct Node2 *head =NULL;
    struct Node2 *temp=head;
    int num,div;

    cin>>num;
    while(num!=-1)
    {
        if(head==NULL) //happens only first time
        {
            if(prime(num)) //problem if first number is prime
            {
                head=new(std::nothrow) struct Node2;
                head->_data=num;
                head->_down=NULL;
                head->_left=new(std::nothrow)struct Node;
                (head->_left)->_data=num;
            }
            else //not prime number
            {
                div=divisable_by(num);
                head=new(std::nothrow) struct Node2;
                head->_data=div;
                head->_down=NULL;
                head->_left=new(std::nothrow)struct Node;
                (head->_left)->_data=num;
            }
        }
        else //after the first number is entered
        {
            temp=head;
            if(prime(num)) //if it is prime
            {
                if(check_in_node2(temp,num)) //number is already in node2
                {
                    if(!check_in_node1(temp-> _left,num))
                        put_in_node(temp->_left,num);
                }

                else //when it is not in node 2 i want to put it in
                {
                    temp=head;
                    put_in_node2(head,num);
                    while(temp->_data!=num)
                    {
                        temp=temp->_down;
                    }
                    put_in_node(temp->_left,num); //current node2
                }
            }
            else //if it is not prime
            {
                div=divisable_by(num);

                if(check_in_node2(temp,div))
                {
                    if(!check_in_node1(temp->_left,num))
                        put_in_node(temp->_left,num);
                }
                else //when i want to enter into node2
                {

                    put_in_node2(head,div);
                    put_in_node(temp->_left,num); //current node2

                }
            }

        }
        cin >> num;
    }
    return head;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//----------------------------------function---------------------------------//
bool prime(int num) //returns true if num is prime
{
    for (int i=2; i<num; i++) //going from 2 until an number before num
    {
        if(num%i==0) //checking if any number divides it
            return false; //if it does its not prime so we return false
    }
    return true; //if it got here no num divided it so its prime
}
//----------------------------------function---------------------------------//
int divisable_by(int num)
{
    for(int i=2; i<num; i++) //going from two till a value before num
    {
        if(prime(i)) //first checking if that num is prime
        {   //if it is we want to check if its a divisor of num
            if(num%i==0) //checking if it divides it
                return i; //if it does we return i
        }
//we will always find an i bec. a none prime number is always
//divisable by prime numbers
    }
}
//----------------------------------function---------------------------------//
bool check_in_node2(struct Node2 *&temp,int search)
//checking if the number is already in the node2's
{
    while(temp!=NULL) //going through boxes
    {
        if(temp -> _data==search) //if value found in curr box
            return true;
        else
            temp=temp->_down; //advance to next box
    }
    return false; //if we got here it was'nt found so we return false
//this function also changes the value of temp with & bec, i want to
//know at which node2 the value was found
}
//----------------------------------function---------------------------------//
bool check_in_node1(struct Node *head,int search)
//func checks if a certain value is already in the line of node's
{

    while(head!=NULL) //going through box
    {
        if(head-> _data ==search) //if value found in curr box
            return true;

        head=head-> _next; //advance to next box
    }
    return false;
}
//----------------------------------function---------------------------------//
void put_in_node(struct Node *&head, int num) //problem here
{
    struct Node *temp=new(std::nothrow) struct Node;
    check_allocated(temp); //checking if allocated correctly
    temp-> _data = num; //problem is around here
    temp -> _next = head;
    head=temp;
//free_list(temp);
}
//----------------------------------function---------------------------------//
void put_in_node2(struct Node2 *&head,int num)
//func creates new box of type node2 and enter's it sorted
{
    Node2 *temp,*front,*rear;


    if(num<head->_data)
    {
        temp=new(std :: nothrow) struct Node2;
        check_allocated2(temp);
        temp->_data=num;
        temp->_down=head;
        head=temp;
    }
    else
    {
        rear=head;
        front=rear->_down;
        while(front!=NULL &&front ->_data<num)
        {
            rear=front;
            front=front->_down;
        }
        temp=new(std :: nothrow) struct Node2;
        check_allocated2(temp);
        temp->_data=num;
        temp->_down=front;
        rear->_down=temp;
    }

}
//----------------------------------function---------------------------------//
void check_allocated(const struct Node * head) //same as func below,
//but for type node
{
    if(head==NULL)
    {
        cerr <<"Cannot allocate memory"<<endl;
        exit(EXIT_FAILURE);
    }

}
//----------------------------------function---------------------------------//
void check_allocated2(const struct Node2 * head) //check if allocated correctly
{
    if(head==NULL) //if its empty, it did'nt allocate memory correctly
    {
        cerr <<"Cannot allocate memory"<<endl; //so we send message
        exit(EXIT_FAILURE); //and exit program with failure
    }
}
//----------------------------------function---------------------------------//
void print_list( struct Node2 ** head)
{
    while(*head!=NULL)
    {
        cout<<(*head)->_data<<" ";
        struct Node * temp=(*head)->_left;
        while(temp!=NULL)
        {
            cout<<temp->_data<<" ";
            temp=temp->_next;
        }
        cout<<endl;
        head=&((*head)->_down);
    }
}
//----------------------------------function---------------------------------//
void free_list2(struct Node2 * head) //deletes allocated type node2
{
    struct Node2 * temp; //helper struct
    while(head!=NULL) //until we get to the end
    {
        free_list(head->_left); //send a line of type node to be freed
        temp=head; //giving address
        head=head->_down; //advancing to next node2
        delete temp; //deleting the last one
    }

}
//----------------------------------function---------------------------------//
void free_list(struct Node * head) //deletes allocated node's
{
    struct Node * temp; //helper struct

    while(head!=NULL) //until the end of chain
    {
        temp=head; //giving address
        head=head->_next; //going to next box
        delete temp; //deleting the previous
    }
}
Thanx eec
The program is working now.
Thanx for the help!
Topic archived. No new replies allowed.