Thursday, 14 April 2016

C And C++ Program to count total number of negative elements in array Problem Solution.

C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,sz,cnt = 0;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++)scanf("%d", &ar[i]);

    for(i = 0; i < sz; i++)if(ar[i] < 0)cnt++;
    printf("\nTotal number of negative elements = %d\n", cnt);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,sz,cnt = 0;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(i = 0; i < sz; i++)cin >> ar[i];

    for(i = 0; i < sz; i++)if(ar[i] < 0)cnt++;
    cout << endl << "Total number of negative elements = " << cnt << endl;;

    return 0;
}

C And C++ Program to store positive elements in an array Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,j,sz,a;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0,j = 0; i < sz; i++){
        scanf("%d", &a);
        if(a >= 0)ar[j++] = a;
    }

    printf("\nElements store in array are : ");
    for(i = 0; i < j; i++)printf("%d ",ar[i]);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],j = 0,sz,a;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++){
        cin >> a;
        if(a >= 0)ar[j++] = a;
    }
    cout << endl << "Elements store in array are : ";
    for(int i = 0; i < j; i++)cout << ar[i] << " ";

    return 0;
}

C And C++ Program to print all negative elements in an array Problem Solution.

C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,sz;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++)scanf("%d", &ar[i]);

    printf("\nAll negative elements in array are : ");
    for(i = 0; i < sz; i++)if(ar[i] < 0)printf("%d ",ar[i]);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],sz;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++)cin >> ar[i];

    cout << endl << "All negative elements in array are : ";
    for(int i = 0; i < sz; i++)if(ar[i] < 0)cout << ar[i] << " ";

    return 0;
}

C And C++ Program to delete all duplicate elements from an array Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],sz,i,j,k;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++)scanf("%d", &ar[i]);

    for(i = 0; i < sz; i++){
        for(j = i+1; j < sz; j++){
            if(ar[i] == ar[j]){
                for(k = j; k < sz-1; k++)ar[k] = ar[k+1];
                sz--;
            }
        }
    }
    printf("\nArray elements after deleting duplicates : ");
    for(i = 0; i < sz; i++)printf("%d ", ar[i]);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],sz;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++)cin >> ar[i];

    for(int i = 0; i < sz; i++){
        for(int j = i+1; j < sz; j++){
            if(ar[i] == ar[j]){
                for(int k = j; k < sz-1; k++)ar[k] = ar[k+1];
                sz--;
            }
        }
    }
    cout << endl << "Array elements after deleting duplicates : ";
    for(int i = 0; i < sz; i++)cout << ar[i] << " ";

    return 0;
}

C And C++ Program count total number of duplicate elements in an array Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[100],i,j,sz,cnt = 0;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++)scanf("%d", &ar[i]);

    for(i = 0; i < sz; i++){
        for(j = i+1; j < sz; j++){
            if(ar[i] == ar[j]){
                cnt++;
                break;
            }
        }
    }
    printf("\nTotal number of duplicate elements found in array = %d", cnt);

    return 0;
}

Another Way:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[100],i,j,sz,cnt[100] = {0},dup = 0,sum = 0,mxRange = -99999;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++){
        scanf("%d", &ar[i]);
        cnt[ar[i]]++;
        if(ar[i] > mxRange)mxRange = ar[i];
    }
    for(i = 0; i < mxRange; i++){
        if(cnt[i] > 1){
            dup += cnt[i]-1;
        }
    }
    printf("\nTotal number of duplicate elements found in array = %d", dup);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[100],sz,cnt = 0;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++)cin >> ar[i];

    for(int i = 0; i < sz; i++){
        for(int j = i+1; j < sz; j++){
            if(ar[i] == ar[j]){
                cnt++;
                break;
            }
        }
    }
    cout << endl << "Total number of duplicate elements found in array = " << cnt << endl;

    return 0;
}

Another Way:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[100],i,j,sz,cnt[100] = {0},dup = 0,sum = 0,mxRange = -99999;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(i = 0; i < sz; i++){
        cin >> ar[i];
        cnt[ar[i]]++;
        if(ar[i] > mxRange)mxRange = ar[i];
    }
    for(i = 0; i < mxRange; i++){
        if(cnt[i] > 1){
            dup += cnt[i]-1;
        }
    }
    cout << endl << "Total number of duplicate elements found in array = " <<  dup << endl;;

    return 0;
}

C And C++ Program to delete an element from an array at specified position Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,sz,pos;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++) scanf("%d", &ar[i]);

    printf("Enter the element position to delete : ");
    scanf("%d", &pos);

    if(pos == sz+1 || pos < 0)printf("Invalid position!\n");
    else for(i = pos-1; i < sz; i++)ar[i] = ar[i+1];

    printf("\nElements of array after delete are :");
    for(i = 0; i < sz-1; i++)printf("%d ", ar[i]);

    return 0;


C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],sz,pos;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++)cin >> ar[i];

    cout << "Enter the element position to delete : ";
    cin >> pos;

    if(pos == sz+1 || pos < 0)cout << "Invalid position!" << endl;
    else for(int i = pos-1; i < sz; i++)ar[i] = ar[i+1];

    cout << endl << "Elements of array after delete are :";
    for(int i = 0; i < sz-1; i++)cout << ar[i] << " ";

    return 0;
}

C And C++ Program to insert an element in array at specified position Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,sz,num,pos;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0; i < sz; i++)scanf("%d", &ar[i]);

    printf("Enter element to insert : ");
    scanf("%d", &num);

    printf("Enter the element position : ");
    scanf("%d", &pos);

    if(pos > sz+1 || pos <= 0)printf("Invalid position!");
    else {
        for(i = sz; i >= pos; i--) ar[i] = ar[i-1];

        ar[pos-1] = num;
        sz++;

        printf("Array elements after insertion : ");
        for(i = 0; i < sz; i++)printf("%d ", ar[i]);
    }

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,sz,num,pos;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(i = 0; i < sz; i++)cin >> ar[i];

    cout << "Enter element to insert : ";
    cin >> num;

    cout << "Enter the element position : ";
    cin >> pos;

    if(pos > sz+1 || pos <= 0)cout << "Invalid position!";
    else {

        for(i = sz; i >= pos; i--) ar[i] = ar[i-1];

        ar[pos-1] = num;
        sz++;

        cout << "Array elements after insertion : ";
        for(i = 0; i < sz; i++)cout << ar[i] << " ";
    }

    return 0;
}

C And C++ Program to copy all elements of one array to another Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int first[1000],second[1000],i,sz;

    printf("Enter the size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements of first array (Separated by space): ");
    for(i = 0; i < sz; i++)scanf("%d", &first[i]);

    printf("Enter elements of second array (Separated by space): ");
    for(i = 0; i < sz; i++)second[i] = first[i];

    printf("\nElements of first array are : ");
    for(i = 0; i < sz; i++)printf("%d ", first[i]);

    printf("\nElements of second array are : ");
    for(i = 0; i < sz; i++)printf("%d ", second[i]);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int first[1000],second[1000],sz;

    cout << "Enter the size of the array : ";
    cin >> sz;

    cout << "Enter elements of first array (Separated by space): ";
    for(int i = 0; i < sz; i++)cin >> first[i];

    for(int i = 0; i < sz; i++)second[i] = first[i];

    cout << endl << "Elements of first array are : ";
    for(int i = 0; i < sz; i++)cout << first[i] << " ";

    cout << endl << "Elements of second array are : ";
    for(int i = 0; i < sz; i++)cout << second[i] << " ";

    return 0;
}

C and C++ Program to find second largest number in an array Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[10000],i,mx1,mx2,sz;

    printf("Enter size of the array: ");
    scanf("%d", &sz);

    printf("Enter elements in the array (Separated by space): ");
    for(i = 0; i < sz; i++) scanf("%d", &ar[i]);

    mx1 = ar[0];
    mx2 = ar[0];

    for(i = 1; i < sz; i++){
        if(ar[i] > mx1){
            mx2 = mx1;
            mx1 = ar[i];
        }
        else if(ar[i] > mx2)mx2 = ar[i];
    }
    printf("First largest = %d\n", mx1);
    printf("Second largest = %d\n", mx2);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[10000],mx1,mx2,sz;

    cout << "Enter size of the array: ";
    cin >> sz;

    cout << "Enter elements in the array (Separated by space): ";
    for(int i = 0; i < sz; i++) cin >> ar[i];

    mx1 = ar[0];
    mx2 = ar[0];

    for(int i = 1; i < sz; i++){
        if(ar[i] > mx1){
            mx2 = mx1;
            mx1 = ar[i];
        }
        else if(ar[i] > mx2)mx2 = ar[i];
    }
    cout << "First largest = " << mx1 << endl;
    cout << "Second largest = " << mx2 << endl;

    return 0;
}