Friday, 25 December 2015

Given A List Of Integers Stored In A Linear Array. Find Out The Summations Of Numbers In Odd Indices And Even Indices Separately Problem Solution Using C++.

/*
Given a list of integers stored in a linear array. Find out the summations of numbers in odd indices and even indices  separately.
Suppose, 0 is a even number than index 0 of an array is an even index.

TAG: C++, Algorithm , Data Structure , Array ,Array Index, Summation Problem , Easy Solution , Implementation Problem.
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,ar[1000],oddSum = 0,evenSum = 0;
    cout << "How Many Element You Want To Take: " << endl;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> ar[i];
        if(i%2 == 0)evenSum+=ar[i];
        else oddSum+=ar[i];
    }
    cout << "Summation Of Even Numbers Is: " << evenSum << endl;
    cout << "Summation Of Odd Numbers Is: " << oddSum << endl;
    return 0;
}

Given A List Of Integers Stored In A Linear Array And A Data Element, Insert The Element Into The Array At A Given Position Problem Solution Using C++.

/*
Given a list of integers stored in a linear array and a data element, insert the element into the array at a given position.

TAG: C++, Algorithm , Data Structure , Array ,Array Indexing, Summation Problem , Easy Solution , Implementation Problem.
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,newEle,indexPos;
    cout << "How Many Element You Want To Take: " << endl;
    cin >> n;
    int ar[n];
    for(int i = 0; i < n; i++){
        cin >> ar[i];
    }
    cout << "Enter New Element : " << endl;
    cin >> newEle;
    cout << "Enter Position Of The Array: " << endl;
    cin >> indexPos;
    ar[n+1];
    int j = 0,tmpAr[n+1];
    for(int i = indexPos-1; i < n; i++){
        tmpAr[j++] = ar[i];
    }
    ar[indexPos-1] = newEle;
    for(int i = indexPos,j = 0; i < n+1; i++)ar[i] = tmpAr[j++];
    cout << "Array Elements Are: " << endl;
    for(int i = 0; i < n+1; i++){
        cout << ar[i] << " ";
    }
    cout << endl;
    return 0;
}

Given A List Of Integers Stored In A Linear Array, Delete A Data From A Given Position Of The Array Problem Solution Using C++.

/*
Given a list of integers stored in a linear array, delete a data from a given position of the array.

TAG: C++, Array Easy Solution Array Indexing , Implementation.
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,ar[1000],index;
    cout << "Enter How Many Number You Want To Take: " << endl;
    cin >> n;
    for(int i = 0; i < n; i++) cin >> ar[i];
    cout << "Enter Delete index: " << endl;
    cin >> index;
    for(int i = index-1; i < n-1; i++)ar[i] = ar[i+1];
    cout << "Final Array List Is: ";
    for(int i = 0; i < n-1; i++)cout << ar[i] << " ";
    cout << endl;
    return 0;
}

Given Two Linear Array Of Integers, Merge The Two Array Into A Single Array Problem Solution Using C++.

/*
Given two linear array of integers, merge the two array into a single array.

TAG: C++, Array Easy Solution Array Indexing ,Merge Implementation.
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int first,second,firstArray[100],secondArray[100];
    cout << "Enter How Many Element You Want To Take For First Array: ";
    cin >> first;
    cout << endl << "Enter First Array Element: ";
    for(int i = 0; i < first; i++)cin >> firstArray[i];
    cout << endl << "Enter How Many Element You Want To Take For Second Array: ";
    cin >> second;
    cout << endl << "Enter Second Array Element: ";
    for(int i = 0; i < second; i++) cin >> secondArray[i];
    int singleArray[first+second],j,k;
    for(int i =0, j = 0,k = 0; i < first+second; i++,j++){
        if(j < first)singleArray[i] = firstArray[j];
        else singleArray[i] = secondArray[k++];
    }
    cout << endl << "Marge Array Element Is: ";
    for(int i = 0; i < first+second; i++)cout << singleArray[i] << " ";
    cout << endl;
    return 0;
}

Given a two-dimensional array, find out the summation of the boundary elements of the array. Here no element should added twice Problem Solution Using C++.

/*
Given a two-dimensional array, find out the summation of the boundary elements of the array. Here no element should added twice.

Interpretation: Boundary Elements are those elements whose index i = 0 or j = 0, and those whose index i = n-1 or j = m-1 and add it with sum.

if n = 3, m = 3

--------------------------------
     1   |     2      |    3
--------------------------------
     4   |     5      |    6
--------------------------------
     7   |          |    9
--------------------------------
The Red Indexes Are Boundary Of A 2D Array.

TAG: C++, 2D Array Easy Solution, 2D Array Indexing , Implementation , Boundary Element Of An 2D Array.

*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,m,ar[100][100],sum = 0;
    cout << "Enter Row: ";
    cin >> n;
    cout << endl << "Enter Column: ";
    cin >> m;
    cout << endl << "Enter Array Element: " << endl;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> ar[i][j];  // Storing Element in 2D Array
            if(i == 0 || j == 0 || i == n-1 || j == m-1)sum+=ar[i][j];//Calculating Boundary Element
        }
    }
    cout << endl << "Summation Of Boundary Element Is: " << sum << endl;
    return 0;
}

Given A Two Dimensional Array, Find Out The Summation Of Diagonal Element Of The Array Problem Solution Using C++.

/*
Given a two dimensional array, find out the summation of diagonal element of the array.

If, Row = 3 And Col = 3

----------------------------------
1         |      2     |       3
----------------------------------
4         |      5     |       6
----------------------------------
        |      8     |       9
----------------------------------

Here Red Color Indexes Are Array Diagonal Of A 2D Array. If We Calculate The Summation Of The Diagonal  Than We Have To Take 5 One Time.

TAG: C++, 2D Array Easy Solution, 2D Array Indexing , Implementation , Diagonal Element Of An 2D Array.

*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int row,col,ar[100][100],sum = 0;

    cout << "Enter Row : ";
    cin >> row;

    cout << "Enter Column : ";
    cin >> col;

    cout << endl << "Enter Array Element: " << endl;
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            cin >> ar[i][j];
            if(i == j || (i+j) == col-1)sum+=ar[i][j];
        }
    }
    cout << endl << "Summation Of Diagonal Element Is: " << sum << endl;
    return 0;
}

There Are 40 Students In Class. They Have Written 4 Class Tests Of A Course. Find Out The Average Of The Best 3 Class Tests For Each Student Problem Solution Using C++.

/*
There are 40 students in class. They have written 4 class tests of a course. Find out the average of the best 3 class tests for each student.

TAG: C++, 2D Array Easy Solution, Summation Problem, Implementation .

*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int student[41][4];
    float avgMarks[41][1];

    cout << "Enter Student Marks: " << endl;
    for(int i = 0; i < 40; i++){
        cout << "Student[" << i+1 << "]: ";
        int min_marks = 9999999,sum = 0; // Set min_marks As a Big Value
        for(int j = 0;j < 4;j++){
            cin >> student[i][j];  // Input Student Numbers
            if(student[i][j] < min_marks)min_marks = student[i][j]; // Store minimum value in min_marks
            sum+=student[i][j]; // Sum All Marks
        }
        avgMarks[i][0] = (float)((sum-min_marks)/3.0); // Deduct Minimum Value From Sum And Divided By 3
    }
    for(int i = 0; i < 4; i++){
        cout << "Student: " << i+1 << " Average Marks: " << avgMarks[i][0] << endl;
    }
    return 0;
}

Write A Program To Enter 10 Integers In A One Dimensional Array And Display (Print) The Data On The Screen Problem Solution Using C++.

/*Write a program to enter 10 integers in a one dimensional array and display (print) the data on the screen.

TAG: C++, Linear Array Easy Solution , Implementation .
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[10];
    cout << "Enter 10 Element: " << endl;
    for(int i = 0; i < 10; i++)cin >> ar[i];
    cout << endl << "The Elements Are: ";
    for(int i = 0; i < 10; i++)cout << ar[i] << " ";
    cout << endl;
    return 0;
}

Write A Program To Store 10 Integers Using An Array And Find Out The Summation And Average Of The Numbers. Display The Numbers, Their Sum And Average (With Points) Separately Problem Solution Using C++.

/*
Write a program to store 10 integers using an array and find out the summation and average of the numbers. Display the numbers, their sum and average (with points) separately.

TAG: C++, Linear Array Easy Solution, Summation And Average Problem, Implementation .
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[10],sum = 0;
    cout << "Enter 10 Number: " << endl;
    for(int i = 0; i < 10; i++){
        cin >> ar[i];
        sum+=ar[i];
    }
    cout << endl << "Summation Of The Element IS: " << sum << endl;
    cout << "Average Of The Element Is: " << sum/10.0 << endl;
    return 0;
}

Write A Program To Find Out The Largest And Smallest Of A Given List Of Numbers In An Array. Display The Numbers, Their Largest And Smallest Separately Problem Solution Using C++.

/*
Write a program to find out the largest and smallest of a given list of numbers in an array. Display the numbers, their largest and smallest separately

TAG: C++, Linear Array Easy Solution, Searching Problem, Implementation Smallest Element Of An Array , Largest Element Of An Array.
*/


Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,ar[100],largest = 0,smallest = 999999999;
    cout << "Enter How Many Number You Want To Take: ";
    cin >> n;
    cout << endl << "Enter Array Element: ";
    for(int i = 0; i < n; i++){
        cin >> ar[i];
        if(ar[i] > largest)largest = ar[i];
        if(ar[i] < smallest)smallest = ar[i];
    }
    cout << "Largest Element Of The Array Is: " << largest << endl;
    cout << "Smallest Element Of The Array Is: " << smallest << endl;
    return 0;
}

Write A Program To Find Out The Summations Of The Numbers Stored In Even Indices And Odd Indices Of An Array. Display The Numbers, The Two Summations Separately Problem Solution Using C++.

/*
Write a program to find out the summations of the numbers stored in even indices and odd indices of an array. Display the numbers, the two summations separately.

TAG: C++, Linear Array Easy Solution, Summation Problem, Array Indexing, Implementation Even,Odd.
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int n,ar[1000],oddSum = 0,evenSum = 0;
    cout << "How Many Element You Want To Take: " << endl;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> ar[i];
        if(i%2 == 0)evenSum+=ar[i];
        else oddSum+=ar[i];
    }
    cout << "Summation Of Even Numbers Is: " << evenSum << endl;
    cout << "Summation Of Odd Numbers Is: " << oddSum << endl;
    return 0;
}

Define A Record (Structure) With Three Fields For A Student And Store Data For 30 Students Problem Solution Using C++.

/*
Define a record (structure) with three fields for a student and store data for 30 students.

TAG: C++, Structure Easy Solution ,Struct  Implementation , Data Structure.

*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct{
    int roll;
    char name[30];
    float cgpa;
}student[30];
int main(){

    cout << "Enter Student Data: " << endl;
    for(int i = 0; i < 30; i++){
        cout << "Enter Student Roll: ";
        cin >> student[i].roll;
        cout << "Enter Student Name: ";
        cin >> student[i].name;
        cout << "Enter Student CGPA: " ;
        cin >> student[i].cgpa;
    }
    cout << endl << "Displaying Student Data: " << endl;
    for(int i = 0; i < 30; i++){
        cout << "Roll: " << student[i].roll << "  Name: " << student[i].name;
        cout << "  CGPA: " << student[i].cgpa << endl;
    }
    return 0;
}

Write A Program To Create A Linked List Of Five Nodes Where Each Node Of The List Will Contain An Integer. Display The Data Of The List On The Screen Problem Solution Using C++.

/*Write a program to create a linked list of five nodes where each node of the list will contain an integer. Display the data of the list on the screen.

TAG: C++, Data Structure ,Easy Problem, Linked List , Linked List Implementation , Input , Output , Singly / Single Linked List ,Singly Linked List Implementation. 

*/

Solution:


#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct Node{
    int data;
    Node *next;
};
int main(){

    int i,n,item;
    Node *newnode,*tmpnode,*firstnode;
    firstnode = NULL;

    cout << "Enter Number Of Node:" << endl;
    cin >> n;

    cout << "Enter Data For Node With Space:";
    for(int i = 0; i < n; i++){
        cin >> item;
        newnode = new Node;
        newnode->data = item;
        newnode->next = NULL;

        if(firstnode == NULL){
            firstnode = newnode;
            tmpnode = newnode;
        }
        else{
            tmpnode->next = newnode;
            tmpnode = newnode;
        }
    }

    cout << endl << "Displaying Node: ";
    tmpnode = firstnode;

    for(int i = 0; i < n; i++){
        cout << tmpnode->data << " ";
        tmpnode = tmpnode->next;
    }
    cout << endl;
    return 0;
}

Output: 
          
          Enter Number Of Node:
          10
          Enter Data For Node With Space:1 2 3 4 5 6 7 8 9 10



          Displaying Node: 1 2 3 4 5 6 7 8 9 10

Write A Program To Search A Node From A Linked List Problem Solution Using C++.

/*
Write a program to search a node from a linked list.

TAG: C++, Solution, Data Structure ,Easy Problem, Linked List , Linked List Implementation , Input , Output , Singly / Single Linked List ,Singly Linked List Implementation, Searching A Node From Linked List ,Searching Problem. 

*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct Node{
    int data;
    Node *next;
};
int main(){

    int n,newData,searchData;
    Node *tmpNode,*newNode,*firstNode;
    firstNode = NULL;

    cout << "Enter Number Of Node: ";
    cin >> n;

    cout << endl << "Enter Data For Node With Space: ";
    for(int i = 0; i < n; i++){
        cin >> newData;
        newNode = new Node;
        newNode->data = newData;
        newNode->next = NULL;

        if(firstNode == NULL){
            firstNode = newNode;
            tmpNode = newNode;
        }
        else{
            tmpNode->next = newNode;
            tmpNode = newNode;
        }
    }

    cout << endl << "Enter Your Searching Node: ";
    cin >> searchData;

    tmpNode = firstNode;
    while(tmpNode->data != searchData && tmpNode->next != NULL)tmpNode = tmpNode->next;

    if(tmpNode->data == searchData)cout << endl << "Data Node" << searchData << " Found." <<endl;
    else cout << endl << "Data Node" << searchData << " Not Found." <<endl;

    return 0;
}

Output:

            Enter Number Of Node: 10


            Enter Data For Node With Space: 1 2 3 4 5 6 7 8 9 10


            Enter Your 
Searching Node: 10



            
Data Node 10 Found.

Write A Function For Node Creation, Another For Linkage And Another Function For Display The Data. Call Them To Show The Result Of Execution Problem Solution Using C++.

/*
Write a function for node creation, another for linkage and another function for display the data. Call them to show the result of execution

TAG: C++, Data Structure ,Easy Problem, Function, Linked List , Linked List Implementation , Input , Output , Singly / Single Linked List ,Singly Linked List Implementation. 
*/

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct Node{
    int data;
    Node *next;
};
Node *newNode,*tmpNode,*firstNode;

void createNewNode(int newData){

    newNode = new Node;
    newNode->data = newData;
    newNode->next = NULL;
}
void linkageNode(){

    if( firstNode == NULL){
        firstNode = newNode;
        tmpNode = newNode;
    }
    else{
        tmpNode->next = newNode;
        tmpNode = newNode;
    }
}
void DisplayNode(int n){

    tmpNode = firstNode;
    cout << endl << "Displaying Node Data: ";

    for(int i = 0; i < n; i++){
        cout << tmpNode->data << " ";
        tmpNode=tmpNode->next;
    }
    cout << endl;
}
int main(){

    int n,newData;

    cout << "Enter Number Of Node: ";
    cin >> n;

    firstNode = NULL;

    cout << endl << "Enter Data For Node With Space: ";
    for(int i = 0; i < n; i++){
        cin >> newData;
        createNewNode(newData);
        linkageNode();
    }
    DisplayNode(n);
    return 0;
}

Output:
          
           Enter Number Of Node: 10

           Enter Data For Node With Space: 1 2 3 4 5 6 7 8 9 10

           Displaying Node Data: 1 2 3 4 5 6 7 8 9 10



Another Way (Hard Way) :

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct Node{
    int data;
    Node *next;
};
Node *createNewNode(int newData,Node *newNode){

    newNode = new Node;
    newNode->data = newData;
    newNode->next = NULL;
    return newNode;

}
Node *linkageNode(Node *newNode, Node *tmpNode, Node **firstNode){

    if( *firstNode == NULL){
        *firstNode = newNode;
        tmpNode = newNode;
    }
    else{
        tmpNode->next = newNode;
        tmpNode = newNode;
    }
    return tmpNode;

}
void DisplayNode(int n,Node *tmpNode,Node *firstNode){

    tmpNode = firstNode;
    cout << endl << "Displaying Node Data: ";
    for(int i = 0; i < n; i++){
        cout << tmpNode->data << " ";
        tmpNode=tmpNode->next;
    }
    cout << endl;

}
int main(){

    int n,newData;
    Node *newNode,*tmpNode,*firstNode;

    cout << "Enter Number Of Node: ";
    cin >> n;

    firstNode = NULL;

    cout << endl << "Enter Data For Node With Space: ";
    for(int i = 0; i < n; i++){
        cin >> newData;
        newNode = createNewNode(newData,newNode);
        tmpNode = linkageNode(newNode,tmpNode,&firstNode);
    }
    DisplayNode(n,tmpNode,firstNode);
    return 0;
}

Output:

           Enter Number Of Node: 10

           Enter Data For Node With Space: 1 2 3 4 5 6 7 8 9 10



           Displaying Node Data: 1 2 3 4 5 6 7 8 9 10