Wednesday, February 12, 2014

Dynamic Array Increasing as needed in C++

Hi Guys Here I am back with another time pass activity
Many of you have heard of data structures like linked lists and binary trees. All of them have their own advantages and usefulness. So for beginners and learners like me I made up this small segment of code that can help increase your array size dynamically and only as much as needed. The Code to delete a specific entry is also very similar just try to note the index and neglect that entry when copying code to another array and definitely decrease its size as well... So all of you guys waiting for code and desperate for me to share should try to understand the logic as well and implement this differently because it is very much useless if you just copy and paste the code.
So Here it is Copy and Paste, Correct and Improve any ambiguity.

 /*----------Ali Azam Rana----------------
The Code is free to be copied and moved and distributed by anyone
Changes and improvements are welcome
*******************************************************
Contact:- aliazam.rana@gmail.com
            fb.com/ali.rana007
            +92-300-662-928-2*/


#include <iostream>
#include <string>

using namespace std;

int main()
{
    int *p1 = nullptr, *p2 = nullptr;//two pointers allocating memory as needed
    int size = 0;
    string input;//input taken into string and then converted to int better control of loops
    p1 = new int[size+1];//partial allocation for first value only
    int con = 0;//converted integers are returned here
    do//loop start
    {

        cout << "\nEnter A Number";
        cin >> input;
        if (input[0] != 'q' && input[0] != 'Q')
        {
            con = stoi(input);
        }
        
        if (size == 0)
        {
            p1[0] = con;
        }
        else
        {
            if (size == 1 || p2 == nullptr)
            {
                p2 = new int[size + 1];
                for (int i = 0; i < size; i++)
                {
                    p2[i] = p1[i];
                }
                p2[size] = con;
                delete[]p1;
                p1 = nullptr;
            }
            else
            {
                p1 = new int[size + 1];
                for (int i = 0; i < size; i++)
                {
                    p1[i] = p2[i];
                }
                p1[size] = con;
                delete[]p2;
                p2 = nullptr;
            }
            
        }
        size++;
        

    } while (input != "q" && input != "Q");
    size--;
    if (p2 == nullptr)
    {
        for (int i = 0; i < size; i++)
            cout << endl << p1[i] << endl;
    }
    else
    {
        for (int i = 0; i < size; i++)
            cout << endl << p2[i] << endl;
    }

    
    cout << endl << endl;
    
    system("pause");
    return 0;
}

Sunday, February 2, 2014

Bubble Sort on Text File in C++

I have tried to implement very simple bubble sort algorithm on files its because many of us beginners struggle with filing in the beginning and sorting in a same file without temporary files is a problem I have used the integers Id here to help you guys solve the problem you can use strings and characters as well string's individual characters can be accessed by the [ ] operator so putting in 0 will give you the first element. So you can find many more things to do here is the code you all have been wanting so long.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;
/******************CREATED by ALI AZAM RANA************************
*************aliazamrana@outlook.com******************************
-----------------------------------------------------------------*/



int main()
{
    fstream file;//use anyfile name
    file.open("Text1.txt", fstream::in | fstream::out);
   
//Contents of File I supposed
/*Id        Name            Number
78        Ali                0300-6629282
661        Xy                965555516516
82        adsa            263536153151
1        dasfa            656516531
*/
 string temp;
    int counter = 0;
    while (!file.eof())// loop to count number of lines in file
    {
        getline(file, temp);
        counter++;    
    }
    file.close();//closing file
    file.open("Text1.txt", fstream::in | fstream::out);//reopening
    counter = counter - 1; //in my file firstline was of headings so I subtracted 1 from it

    for (int i = 1; i < counter; i++)//outer loop for bubble sort
    {
        int id1, id2;//two id variable of int type to store ids sorting via them
        string detail1;//to hold the rest of the record details for ids
        string detail2;
        getline(file, temp);//remember that line of headings on top
        for (int x = 0; x < counter - i; x++)
        {
            
            streampos temPos1, temPos2;// two streamposition objects to hold the file pointer address
            temPos1 = file.tellg(); getting the streamposition first
            file >> id1;//getting id
            getline(file, detail1);//getting details of records
            temPos2 = file.tellg();now checking the file pointer position and storing it
            file >> id2;
            getline(file, detail2);
            
            if (id1 < id2)//swap is happening here 
            {
                int x = id1;
                string y = detail1;
                detail1 = detail2;
                detail2 = y;
                id1 = id2;
                id2 = x;
                file.seekg(temPos1, ios_base::beg);// moving pointer back to the start of the record to be overwritter
                file << id1 << detail1;//swapped ids and details written
                file << endl;                file << id2 << detail2;
            }
            file.seekg(temPos2, ios_base::beg);moving file pointer to the proper place of the last record
            
        }
        file.seekg(0, ios_base::beg);//back to start of the file
    }
    

    cout << endl;
    system("pause");
    return 0;
}