[ create a new paste ] login | about

Link: http://codepad.org/1wADprks    [ raw code | output | fork ]

C++, pasted on Oct 11:
#include<iostream>
#include<fstream>

using namespace std;

int* newArray(char *filename, int &length){
    static int arr[100];
    int number;
    ifstream filein;
    filein.open(filename);

    for(int i = 0; i<length; i++){
        filein >> number;
        arr[i] = number;
    }
    filein.close();
    return arr;
}

void printArray(int arr[], int length){
    for(int i=0; i < length; i++){
        cout << arr[i] << " ";
    }
}

int insertNum(int arr[],int &length, int value, int index){
    if(index>=length || index<0) return 0;
    else{
        for(int i = length; i >= index; i--){
            arr[i] = arr[i-1];
        }
        arr[index] = value;
        length++;
        printArray(arr, length);
    }
    return 0;
}

int removeIndex(int arr[], int &length, int index){
    if(index>=length || index<0) return 0;
    else{
        for(int i=index; i<length; i++){
            arr[i] = arr[i+1];
        }
        length--;
        printArray(arr, length);
    }
    return 0;
}

int findIndex(int arr[], int length, int value){
    for(int i=0; i<length; i++){
        if(arr[i] == value) return i;
    }
    return -1;
}

int main(){
    int mang[100];
    
    int dodai, vitri;
    char ten[20];

    cout << "\nNhap do dai mang:";
    cin >> dodai;

    cout << "\nVao ten file:";
    cin >> ten;

    int *ptr = newArray(ten, dodai);
    printArray(ptr, dodai);

    int value, index, removingIndex;
    cout << "\nNhap so can chen vao mang:";
    cin >> value;
    cout << "\nNhap vi tri can chen:";
    cin >> index;
    insertNum(ptr, dodai, value, index);

    cout << "\nNhap vi tri can xoa";
    cin >> removingIndex;
    removeIndex(ptr, dodai, removingIndex);

    cout << "\nNhap so can tim";
    cin >> value;
    vitri = findIndex(ptr, dodai, value);
    cout << "\nVi tri cua so can tim la: " << vitri << endl;
}


Output:
1
2
3
4
5
6
7
8

Nhap do dai mang:
Vao ten file:
Nhap so can chen vao mang:
Nhap vi tri can chen:
Nhap vi tri can xoa
Nhap so can tim
Vi tri cua so can tim la: -1


Create a new paste based on this one


Comments: