Showing posts with label array programs. Show all posts
Showing posts with label array programs. Show all posts

Friday, 22 January 2016

How to count number of lines in text file in C++ - cppexamples

Leave a Comment


Questions:

How to count number of lines in text file in C++

Explanation:

In this program you will learn how to count number of line in a text file.


Code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {

       fstream file;//creating txt file
       file.open("Reading.txt",ios::in); //opening file in reading mod Reading.txt file is located in your project folder
       int temp2 = 0; //counter
       cout << "Data In Reading File" << endl;
       while (file) {
              string temp;
              getline(file, temp);
              cout << temp << endl;
              temp2++;
       }
       cout << "Number of Lines == " << temp2<<endl;

}


Output:

How to count number of lines in text file in C++
Reading.txt



How to count number of lines in text file in C++
Code Output


Related Articles:

C Plus Plus Program to Create a File & Store Information

C Plus Plus Program to Illustrate Reading of Data from a File


Read More

Wednesday, 5 August 2015

Recursive C Plus Plus program to linearly search an element in a given array

Leave a Comment
Questions:

Recursive C Plus Plus program to linearly search an element in a given array

Code:

#include<iostream>
using namespace std;

/* Recursive function to search x in arr[l..r] */
int recSearch(int arr[], int l, int r, int x)
{
       if (r < l)
              return -1;
       if (arr[l] == x)
              return l;
       return recSearch(arr, l+1, r, x);
}

int main()
{
       int arr[] = {12, 34, 54, 2, 3}, i;
       int n = sizeof(arr)/sizeof(arr[0]);
       int x = 3;
       int index = recSearch(arr, 0, n-1, x);
       if (index != -1)
              cout<<"Element "<<x<<" is present at index "<<index<<endl;
       else
              cout<<"Element"<<x<<" is not present"<<endl;
       return 0;
}



Output

Read More

C Plus Plus function to find maximum in arr[] of size n

Leave a Comment
Questions:

C Plus Plus function to find maximum in arr[] of size n

Code:

// C Plus Plus function to find maximum in arr[] of size n
#include<iostream>
using namespace std;
int largest(int arr[], int n)
{
    int i;
   
    // Initialize maximum element
    int max = arr[0];

    // Traverse array elements from second and
    // compare every element with current max 
    for (i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];

    return max;
}

int main()
{
    int arr[] = {10, 324, 45, 90, 9808};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout<<"Largest in given array is "<< largest(arr, n)<<endl;
    return 0;
}



Output


Read More

C plus plus Program to find sum of elements in a given array

Leave a Comment
Questions:

C plus plus Program to find sum of elements in a given array

Code:


/* C plus plus Program to find sum of elements in a given array  */
#include<iostream>
using namespace std;
#define MAX_SIZE 100

// C plus plus program to return sum of elements in an array of size n
int sum(int arr[], int n)
{
    int sum = 0; // initialize sum
    int i;

    // Iterate through all elements and add them to sum
    for (i = 0; i < n; i++)
       sum +=  arr[i];

    return sum;
}

int main()
{
    int arr[MAX_SIZE];
    int n, i;
    cout<<"Eneter the number of elements in array\n";
    cin>>n;
    cout<<"Enter Elements \n";
    for (i = 0; i < n; i++)
    cin>>arr[i];
    cout<<"Sum of the elements is "<<sum(arr, n);
    return 0;
}




Output


Read More