-->





Translate

Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Append data into text file c programming source code

Leave a Comment
Write a C/C++ program to append a file in which user will enter the file name to open it then enters data to append it. Program should terminate on pressing escape key. When user press enter data should be enter in new line in text file. Use any Compiler CodeBlocks recommended.
Concept used:
Char Array for file name
Loop: while loop for enter data till escape is not pressed
File pointer  FILE * fp; pointer to file
getche() : To get character from console

For basic file input read: write data into  a text file c++ program

Program Explanation:
  • A character array of filename[20] having size equal to 20 used to enter the name of file which has to be appended. If a file is not present of name user entered then a new file will be created in the directory of your .cpp file
  • To append file "a" mode is used in fopen function
  • While loop is used to enter character till escape key is not pressed. When user presses escape a space is added into text file
  • If user presses enter key then an end line is added to text file
C Source Code

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
    FILE *fp;
    char ch, fileName[20];

    printf("Enter name of file with extension to append data e.g test.txt:  ");
    scanf("%s",&fileName);

    fp = fopen(fileName,"a");

    if(fp == NULL){
        printf("File Does Not Exist");
        }else {
        printf("Type data press escape to finish: \n\n");
        }

    printf("\t\t");
    while(ch!=27) {

      ch=getche();
      if(ch==27) {
         putc(' ',fp);
      }
      else {
         putc(ch,fp);
      }
      if(ch==13) {
         cout<<"\n";
         putc('\n',fp);

      }

    }

    fclose(fp);
    cout<<"\n\n\n";
    return 0;
}

Input output of program
append data into text file c programming source code

Read More...

c++ program to add two matrices

Leave a Comment
Write a c++ program to add two matrix using 2-D arrays use any c++ compiler, turbo c++, visual studio or codeblocks.
2D arrays are just like tables which consist of rows and colums and each cell contains a value.
Matrix addition as


1     2         1     2           1+1     2+2                       2         4
            +                = >                           Result=
3     4         3     6          3+3      4+6                       6        10


Concept used:
  • const int  (Important concept here for array size)
  • 2D arrays
  • Nested for loops


Program explanation:
  • For 2D array size there must be constant value in square brackets like
    array[constant value][constant value]
  • Two const variables row and col are used to define size
    if we do not make both const then error found because without const reserve word they are
    behaving as variable.
  • Before placing both variable in square brackets they must initialized else error will be found
  • three nested for loops are used two for taking input in matrix 2D arrays and one for resultant matrix

c++ code
compiler:  
codeblocks c++ compiler



#include<iostream>
using namespace std;

int main(){

//Using const int for array size
    const int row=2,col=2;
// if not use const error found

cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For First Matrix:"<<endl;

int first[row][col], second[row][col];

int i,j;
fori=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;
    forj=0;j<col;j++){
        cin>>first[i][j];
    }

}


cout<<"\n\n\nEnter Value For Second Matrix:"<<endl;
fori=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;

    forj=0;j<col;j++){
        cin>>second[i][j];

    }

}

// Resultant Matrix
fori=0;i<row;i++){

    forj=0;j<col;j++){
        first[i][j]=first[i][j]+second[i][j];

    }

}

cout<<"\n\n\t\tResultant Matrix:"<<endl;
fori=0;i<row;i++){
    cout<< endl;
    forj=0;j<col;j++){
        cout<<"\t\t"<<first[i][j]<<"    ";

    }

}
return 0;
}



sample input out:
c++ program to add two matrices using 2D array




This program is helpful for c++ beginners to understand the basic working of 2D arrays. It is recommended to change the code and examine the output.
Read More...