Linked List - Adding/Displaying using a vector - c++

I have a homework to do but I don't know what's the problem with the displaying function.. I have a vector use to make more linked list (it's a part of the homework).. that's the code:
#include <iostream>
using namespace std;
#define MAX_LIST 100
struct nod {
int info;
nod* urm;
};
nod* liste[MAX_LIST];
void citesteListaSimpla(nod* liste[MAX_LIST],int nrListe);
void afisareListaSimpla(nod* liste[MAX_LIST],int nrListe);
int main()
{
unsigned int nrListe;
cout << "List numbers: ";
cin>>nrListe;
cout<<endl;
citesteListaSimpla(liste,nrListe);
afisareListaSimpla(liste,nrListe);
}
void citesteListaSimpla(nod* liste[MAX_LIST],int nrListe)
{
for(int i=0; i<nrListe; i++)
{
unsigned int nrElemente;
cout<<"Numbers of the list "<< i+1 << ": ";
cin>>nrElemente;
int element;
liste[i]=NULL;
nod* liste[nrListe];
for(int j=1; j<=nrElemente; j++)
{
cout<<"Number "<<j<<": ";
cin>>element;
liste[nrListe]=new nod;
liste[nrListe]->info=element;
liste[nrListe]->urm=liste[i];
liste[i]=liste[nrListe];
}
}
}
void afisareListaSimpla(nod* liste[MAX_LIST],int nrListe)
{
for(int i=0; i<nrListe; i++)
{
nod* liste[nrListe];
liste[nrListe]=liste[i];
while(liste[nrListe]!=NULL)
{
cout<<liste[nrListe]->info<<", ";
liste[nrListe]=liste[nrListe]->urm;
}
}
}
And if I run it it looks like this:
How I make the program to show me the linked list ? ..

On line 35 and 52 you're re-declaring liste shadowing the function parameter. Having liste declared in three different scopes is a recipe for disaster.
nod* liste[nrListe];
Removing those two lines seems to work.
You should probably use a better editor (I use CLion). You would have figured this out, because CLion warned me about it when I pasted your code in the editor. ;)

Related

Code Blocks gives me following error upon execution of code(even parity hamming code)

error message
This is what it shows in the build log:
Checking for existence: C:\CodeBlocks\hammingcodeven.exe
Executing: '"C:\CodeBlocks/cb_console_runner.exe" "C:\CodeBlocks\hammingcodeven.exe"' (in 'C:\CodeBlocks')
Set variable: PATH=C:\MinGW\bin;C:\MinGW;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Users\mahek\AppData\Local\Microsoft\WindowsApps
Process terminated with status -1073741510 (0 minute(s), 54 second(s))
This is the code to find binary value of m bit code after using an even parity hamming code
#include<iostream>
#include<math.h>
using namespace std;
class data
{
int A[50],m,r,ps[6],B[6][56],final[56];
public:
void show();
void input();
void findr();
void findps();
void binary();
void findfinal();
};
void data::input()
{
cout<<"Enter length of frame:";
cin>>m;
cout<<"Enter frame:";
for(int i=1;i<=m;i++)
{
cin>>A[i];
}
findr();
}
void data::findr()
{
r=0;
for(int i=1;i<=m;i++)
{
int x=pow(2,i);
if(x>(m+i+1))
{
r=i;
break;
}
}
if(r==0)
cout<<"Error";
binary();
}
void data::binary()
{
for(int i=1;i<=m+r;i++)
{
int h=i;
for(int j=r;j>=1;j--)
{
B[i][j]=h%2;
h=h/2;
}
}
findps();
}
void data::findps()
{
for(int i=1;i<=r;i++)
ps[i]=0;
for(int i=1;i<=m+r;i++)
{
for(int j=1;j<=r;j++)
{
ps[j]=ps[j]^B[i][j];
}
}
findfinal();
}
void data::findfinal()
{
for(int i=1,j=1;i<=m+r,j<=r;i++,j*=2)
{
if(i==j)
{
final[i]=ps[j];
}
else
final[i]=A[i];
}
}
void data::show()
{
cout<<"Input:";
for(int i=1;i<=m;i++)
cout<<A[i]<<" ";
cout<<endl;
cout<<"Output:";
for(int i=1;i<=m+r;i++)
cout<<final[i]<<" ";
}
int main()
{
data obj;
obj.input();
obj.show();
}
In the first loop of the function binary(), the variable i can go as high as m + r. In your example, m is 8, so m + r is at least 9. However, i is used to index the first dimension of B, which has a sizr of six. Accessing B outside of its defined size results in reading/writing other memory on the stack, resulting in undefined behavior. To help with this, you could dynamically allocate your fields with the needed size. I also strongly recommend giving your fields meaningful names to help people understand what your code is trying to do.
Furthermore, you are indexing arrays incorrectly. In C++ and many other languages, arrays are zero indexed, meaning that the first value of an array A of size n is at A[0] and the last value is at A[n-1].

NFA simulator error C++

I have one big problem with my NFA simulator.
When I run the code sometimes everything goes nice, but sometimes I get this
Process terminated with status -1073741819(0xC0000005)
What do I miss out and what to do to get this work fine?
This is the code:
#include <iostream>
#include<fstream>
#include<map>
using namespace std;
ifstream fin("fisier.txt");
class NFA {
int initiala,finale,stari,tran,cuvinte;
int *f;
multimap <pair <int,int>,char>t;
public:
void stari_finale();
void tranzitii();
void rezolvare();
};
void NFA::stari_finale()
{
fin>>finale;
f=new int[finale];
for(int i=1;i<=finale;i++)
fin>>f[i];
}
void NFA::tranzitii()
{
fin>>tran;
for(int i=1;i<=tran;i++)
{
int x,y;
char c2;
fin>>x>>y>>c2;
t.insert(make_pair(make_pair(x,y),c2));
}
}
void NFA::rezolvare()
{
fin>>stari>>initiala;
fin>>cuvinte;
for(int i=1;i<=cuvinte;i++)
{
int l;
fin>>l;
char *cuv=new char[l+1];
fin.get();
fin.getline(cuv,l+1);
int *c=new int[stari],nr=1;
c[1]=initiala;
for(int j=0;j<l;j++)
{
int *c1=new int[stari];
int n=0;
for(int k=1;k<=nr;k++)
for(int z=0;z<=stari;z++)
if(t.find(make_pair(c[k],z))!=t.end())
if(t.find(make_pair(c[k],z))->second==cuv[j])
n++,c1[n]=z;
for(int k=1;k<=n;k++)
c[k]=c1[k];
nr=n;
delete c1;
}
for(int j=1;j<=nr;j++)
{for(int k=1;k<=finale;k++)
if(c[j]==f[k])
{
cout<<"Word "<<cuv<<" is accepted!\n";
nr=-1;
break;
}
if(nr==-1)
break;
}
if(nr!=-1)
cout<<"Word "<<cuv<<" isn't accepted!\n";
delete c;
delete cuv;
}
}
int main()
{
NFA test;
test.stari_finale();
test.tranzitii();
test.rezolvare();
return 0;
}
One major problem is you are not calling the right delete on your variables. If you call new you need to call delete. If you call new[] you need to use delete[]. Mixing new[] and delete calls will cause undefined behavior which is a symptom of what is happening.
Your calls to delete for c, c1 and cuv should all be delete [] variable_name
You are writing outside the array here:
void NFA::stari_finale()
{
fin>>finale;
f=new int[finale];
for(int i=1;i<=finale;i++)
fin>>f[i];
}
f has the size finale, but i will be equal to finale in the last iteration.
Use this instead:
void NFA::stari_finale()
{
fin>>finale;
f=new int[finale];
for(int i=0;i<finale;i++)
fin>>f[i];
}
or, if you really need to use the 1-based indexing:
void NFA::stari_finale()
{
fin>>finale;
f=new int[finale + 1];
for(int i=1;i<=finale;i++)
fin>>f[i];
}

Structures inside private

I do not know how to work with structures inside a class. I think I have the first part right but nothing works in the main. When I try running my program it says "function does not take 0 arguments" Should I write everything in the main like this:
P.Read(BOX m);
Here's my code so far as below:
#include <iostream>
#include <string>
using namespace std;
template <class T, int n>
class SIX
{
private:
struct BOX
{
T a[n];
string name;
};
public:
void Read(BOX m)
{
cout<<"Enter your name: ";
cin>>m.name;
cout<<m.name<<" please enter "<<n<<" data: ";
for(int i=0;i<n;++i)
cin>>m.a[i];
}
void SortArray(BOX m)
{
sort(m.a, m.a+n);
}
void Display(BOX m)
{
cout<<m.name<<" this is the sorted list of data in your array a: ";
for(int i=0;i<n;++i)
cout<<m.a[i]<<'\t';
cout<<endl;
}
};
int main()
{
SIX <int, 6> P;
SIX <string, 5> Q;
P.Read();
P.SortArray();
P.Display();
cout<<endl;
Q.Read();
Q.SortArray();
Q.Display();
cout<<endl;
system("pause");
return 0;
}
No, define Read as
void Read()
{
cout<<"Enter your name: ";
cin>>BOX.name;
cout<<BOX.name<<" please enter "<<n<<" data: ";
for(int i=0;i<n;++i)
cin>>BOX.a[i];
}
Read is a member function, and it has access to Box, so that's all you have to do.
Same for the other functions, you don't need to specify BOX m as the parameter, replace it by () and replace m by BOX inside the code.
As #Barmar pointed out, your struct BOX just defines a type, you should define a member variable named BOX. Simplest way to do it is to just move BOX to the end of struct declaration,
struct
{
T a[n];
string name;
} BOX;

Program Crashes While Setting Data in Array

I'm working on a project and every time I go to set example[4].m_dArray[3], the program crashes. I can set the value of every other variable up until I get to example[4].m_dArray[3]. Any help would be appreciated!
Prog1Class.h:
#include "Prog1Struct.h"
#pragma once
#include <iostream>
#include <string.h>
using namespace std;
class Prog1Class
{
private:
Prog1Struct example[4];
public:
Prog1Class();
~Prog1Class ();
void setStructData();
void getStructData();
void ptrFunction();
void refFunction();
void printStruct();
void printData();
};
Prog1Struct.h:
#pragma once
#include <string.h>
struct Prog1Struct {
int m_iVal;
double m_dArray[4];
char m_sLine[80];
};
Prog1Class.cpp:
#include "Prog1Class.h"
#include "Prog1Struct.h"
#include <iostream>
#include <string.h>
using namespace std;
Prog1Class::Prog1Class()
{
}
Prog1Class::~Prog1Class()
{
delete &example[4];
}
int main()
{
Prog1Class *aClass = new Prog1Class();
aClass->setStructData();
aClass->printData();
return 0;
}
void Prog1Class::setStructData()
{
for(int i=0;i<5;i++)
{
cout << "Enter an integer: ";
cin >> example[i].m_iVal;
for(int j=0;j<5;j++)
{
cout << endl << "Enter a double: ";
cin >> example[i].m_dArray[j];
}
cout << endl << "Enter a string: ";
cin.ignore(256,'\n');
cin.getline(example[i].m_sLine, 80, '\n');
cout << endl;
}
}
void Prog1Class::getStructData()
{
}
void Prog1Class::printData()
{
for(int i=0;i<5;i++)
{
cout << example[i].m_iVal;
for(int j=0;j<5;j++)
{
cout << example[i].m_dArray[j];
}
cout << example[i].m_sLine;
}
}
You need to change this
class Prog1Class
{
private:
Prog1Struct example[4];
to this
class Prog1Class
{
private:
Prog1Struct example[5];
In C++ arrays start at index 0, so an array of size 4 has valid indexes 0 upto 3. You're using example[4] so you need an array of (at least) size 5.
You also need to remove delete &example[4]; from your destructor as well.
First , delete &example[4]; should be delete [] example;
Second, where did you allocate memory for example?
You need to show the way the example object is declared? I'd suspect some mis-allocated or mis-declared problem with it. Your setStructureData doesn't do anything ostensibly wrong (assuming the array sizes match and the size of the string is fitting - it is a char array, not a pointer, correct?).
for (int i = 0; i < 5; i++)
// ^^^^^
Your for loops should have an ending condition with i < 4 since your arrays have only 4 spaces. Moreover, your delete should be delete[] example;.
Array deletion is delete [] example, not your way. Also, to use delete data needs to be allocated with new
Here is small example how to use new and delete
#include <iostream>
struct foo
{
foo() {std::cout <<"constructed\n";}
~foo() {std::cout <<"destroyed\n";}
};
int main ()
{
foo * pt;
pt = new foo[3];
delete[] pt;
return 0;
}
Also the output:
constructed
constructed
constructed
destroyed
destroyed
destroyed
I gave a lesson about new/delete and just now seen real problem:
example[4] is out of bounds. If you declare array with 4 elements it means you have indexes 0, 1, 2, 3 and nothing more.

Newbie - matrix addition implementation in c++

Hello i'm trying to program the addition of 2 matrices into a new one (and it does when i run the program step by step) but for some reason VS 2010 gives me an access error after it does the addition.
Here is the code.
#include <iostream>
#include <cstdio>
#include <conio>
using namespace std;
class operatii
{
typedef double mat[5][5];
mat ms,m1,m2;
int x1,x2,y1,y2;
public:
void preg();
int cit_val();
void cit_mat(int&,int&,double[5][5]);
void suma();
void afisare(int&,int&,double[5][5]);
};
void operatii::preg()
{
cit_mat(x1,y1,m1);
cit_mat(x2,y2,m2);
suma();
afisare(x1,y1,ms);
}
int operatii::cit_val()
{
int n;
cin>>n;
return n;
}
void operatii::cit_mat(int& x,int& y,double m[5][5])
{
char r;
cout<<"Matrice patratica? Y/N ";
cin>>r;
if ((r=='y')||(r=='Y'))
{
cout<<"Numar linii si coloane: ";
x=cit_val();
y=x;
}
else
{
cout<<"Numar linii: ";
x=cit_val();
cout<<"Numar coloane: ";
y=cit_val();
}
for (int i=1;i<=x;i++)
for (int j=1;j<=y;j++)
cin>>m[i][j];
}
void operatii::suma()
{
if ((x1==x2)&&(y1==y2))
for (int i=1;i<=x1;i++)
for (int j=1;i<=y1;j++)
ms[i][j]=m1[i][j]+m2[i][j];
else cout<<"Eroare";
}
void operatii::afisare(int& x,int& y,double m[5][5])
{
cout<<endl;
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
cout<<m[i][j];
cout<<endl;
}
}
void main()
{
operatii matrice;
matrice.preg();
system("PAUSE");
}
Any kind of help would be apreciated.
Arrays are 0-based in c++.
Change your various variants of for (somevar=1; somevar<=something) to for (somevar=0; somevar<something)
You're writing past the end of your arrays, which overwrites stack return address, leading to a return to nonexecutable code, again leading to an access violation.
Also,
for (int j=1;i<=y1;j++)
I think you want to use j not i here. Such errors are much easier to see if you use longer and more distinct variable names than "i" and "j", such as e.g. "Line" and "Column"