Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//How do I make the program recursive instead of iterative? My input file (for recursive) and (iterative) code are below. This is my first time using this website so I don't know how I am supposed to ask the questions. I'm also very beginner in code, Sorry.
BTS, DYNAMITE, 3.43, 41000000
NF, CLOUDS, 1.14, 869000
HARRY, FALLING, 3.27, 30000000
RACHEL, FIGHT SONG, 3.23, 166000
RUTH, LOST BOY, 4.33, 65000
JAMES, SAY YOU WONT LET GO, 3.27 213000
HARRY, SIGN OF THE TIMES, 5.41, 63000000
IDENA, LET IT GO, 3.21, 21000000
BTS, BLACK SWAN, 3.43, 23000000
PASSENGER, LET HER GO, 4.20, 97000
KATIE, DAISIES, 2.53, 12000
BEBE, SACRIFICES, 2.40, 300000
ANDY, DONT GIVE UP ON ME, 3.16, 5600000
JUSTIN, BIGGER THAN, 3.28, 21000
SAM, IM READY, 3.20, 30500
ALEC, OH MY GOD, 3.07, 1000000
ANDY, GOOD EXAMPLE, 2.29, 15000
GREYSON, GOOD AS GOLD, 3.25, 78000
BRYNN, TELL ME IM PRETTY, 3.08, 29000
GRYFFIN, CRY, 3.38, 3000000
KHALID, FREE SPIRIT, 3.02, 55000
KYGO, LIKE IT IS, 3.03, 890000
CHAINSMOKERS, THIS FEELING, 3.17, 760000
KASKADE, WITH YOU, 3.00, 2000
LAUV, LIKE ME BETTER, 3.17, 5000000
AVA, SALT, 3.00, 56900
LEWIS, BEFORE YOU GO, 3.50, 89000
LAUV, NEVER NOT, 4.02, 76000
JEREMY, ALWAYS ILL CARE, 2.40, 68000
JAMES, IMPOSSIBLE, 4.07, 49000
GMILLER, NO TEARS LEFT TO CRY, 3.10, 770000

C++
<pre>//Song.h

#pragma once

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

using namespace std;

//define class Song

class Song

{

char *title;

char *artist;

char *album;

char *duration;

public:

Song();

Song(char *t, char *a, char *du, char *al);

//accessor functions

char *getTitle();

char *getArtist();

char *getAlbum();

char *getDuration();

//mutator functions

void setTitle(char *);

void setArtist(char *);

void setDuration(char *du);

void setAlbum(char *);

//destructor

~Song();

};

//end of Song.h

//Song.cpp

#include"Song.h"

Song::Song()

{

title = NULL;

artist = NULL;

album = NULL;

duration = NULL;

}

Song::Song(char *t, char *a, char *du, char *al)

{

//allocate memory for title, artist and album

title = new char[strlen(t)+1];

artist = new char[strlen(a)+1];

album = new char[strlen(al)+1];

duration = new char[strlen(du)+1];

strcpy(title, t);

strcpy(artist, a);

strcpy(album, al);

strcpy(duration, du);

}

Song::~Song()

{

delete title;

delete artist;

delete album;

delete duration;

}

//accessor functions

char *Song::getTitle()

{

return title;

}

char *Song::getArtist()

{

return artist;

}

char *Song::getAlbum()

{

return album;

}

char *Song::getDuration()

{

return duration;

}

//mutator functions

void Song::setArtist(char *a)

{

if(artist != NULL)

delete artist;

artist = new char[strlen(a) + 1];

strcpy(artist, a);

}

void Song::setTitle(char *t)

{

if(title != NULL)

delete title;

title = new char[strlen(t) + 1];

strcpy(title, t);

}

void Song::setDuration(char *du)

{

if(duration != NULL)

delete duration;

duration = new char[strlen(du) + 1];

strcpy(duration, du);

}

void Song::setAlbum(char *al)

{

if(album != NULL)

delete album;

album = new char[strlen(al) + 1];

strcpy(album, al);

}

//end of Song.cpp

// SongList.h

#pragma once

#define _CRT_SECURE_NO_DEPRECATE

#include "Song.h"

struct Node

{

Song *song;

Node *next, *prev;

};

class SongList

{

private:

Node *head, *tail;

public:

SongList(); // make an empty list

void insert(Song s); // function to insert song s into the list

void display(); // display the list of songs

~SongList(); // release the memory allocated to the list

};

//end of SongList.h

// SongList.cpp

#include "SongList.h"

// constructor to make an empty list

SongList::SongList()

{

head = NULL;

tail = NULL;

}

// function to insert Song s in sorted order by title

void SongList::insert(Song s)

{

//Make a new node to contain the Song s

Node *node = new Node;

node->song = new Song(s.getTitle(), s.getArtist(), s.getDuration(), s.getAlbum());

node->next = NULL;

node->prev = NULL;

// empty list, make node the new head

if(head == NULL)

{

head = node;

tail = node;

}

else

{

Node *curr = head; // set curr to head

// loop to find the position of insertion of node

while(curr != NULL)

{

if(strcmp(curr->song->getTitle(), s.getTitle()) > 0) // title of s < title of curr's song, exit the loop

break;

curr = curr->next;

}

if(curr == head) // head node's title > s's title, make node the new head

{

node->next = head;

head->prev = node;

head = node;

}

else if(curr == NULL) // s's title > tail's title, make node the new tail

{

tail->next = node;

node->prev = tail;

tail = node;

}

else // insert node before curr

{

node->next = curr;

node->prev = curr->prev;

curr->prev->next = node;

curr->prev = node;

}

}

}

// function to display the songs list

void SongList::display()

{

if(head == NULL) // empty list

{

cout<<"Empty List"<

}else

{

cout<<"Songs: "<

Node *curr = head;

// loop over the list, displaying the details of each song

while(curr != NULL)

{

cout<<"Title: "getTitle()<

cout<<"Artist: "getArtist()<

cout<<"Album: "getAlbum()<

cout<<"Duration: "getDuration()<

curr = curr->next;

}

}

}

// destructor to release the memory allocated to each node

SongList::~SongList()

{

Node *temp = NULL;

while(head != NULL)

{

temp = head;

head = head->next;

temp->next = NULL;

temp->prev = NULL;

delete(temp->song);

delete(temp);

temp = NULL;

}

head = NULL;

tail = NULL;

}

//end of SongList.cpp

// main.cpp : C++ program to make a SongList

#include

#include

#include

#include "SongList.h"

using namespace std;

int main()

{

//make an input stream object to read from file

ifstream in;

//declare a pointer to array of Song list

SongList songList;

//open the input file

in.open("songs.txt");

//chk if opening the file was successful

if(in.fail())

{

cout << "Not able to open the input file songs.txt" << endl;

return -1;

}

//read the file till the end of file

string t, a, al,du;

char *tl,*ar,*alm,*d;

while (!in.eof())

{

getline(in, t, ';');

getline(in, a, ';');

getline(in, du, ';');

getline(in, al, '\n');

//allocate memory for char *

tl = new char[t.length() + 1];

ar = new char[a.length() + 1];

alm = new char[al.length() + 1];

d = new char[du.length() + 1];

strcpy(tl, t.c_str());

strcpy(ar, a.c_str());

strcpy(alm, al.c_str());

strcpy(d, du.c_str());

songList.insert(Song(tl, ar, d, alm));

}

songList.display();

return 0;

}

//end of main.cpp


What I have tried:

I read the book and I wrote the iterative code above but I'm confused on how to make it recursive. I feel extremely dumb and I have no idea what I'm supposed to do.
Posted
Updated 26-Apr-21 21:01pm
Comments
Richard MacCutchan 27-Apr-21 3:06am    
you have not explained what the program is supposed to do, nor why you think recursion is a good solution.
OriginalGriff 27-Apr-21 3:11am    
To add to what Richard said - you also clearly haven't learned from what I told you to do with your last question: it's still double spaced, it's still unindented code.
How do you expect people to help you if you ignore what they tell you?
armagedescu 25-Aug-21 8:31am    
Any for/while loop can be replaced with a recursive call to current function. Think of iteration as a function. Instead of iterating it can be chained. Practically it doesn't make much sense unless your problem is recursive.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900