Skip to content

Commit

Permalink
functions and array
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshlinux committed Apr 23, 2018
1 parent d28a548 commit cb59491
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 2 deletions.
10 changes: 10 additions & 0 deletions File_handling/duplicate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is my first attempt in c++
hello world
thiis is my third line
and fourth line
and fifth line
and
so
on
this ia again

22 changes: 22 additions & 0 deletions File_handling/duplicate_text_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// program to create duplicate of sameer.txt file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream object;
ofstream object1;
char ch;
object.open("sameer.txt"); // member function of ofstream class
object1.open("PRN");
while(!object.eof())
{
object.get(ch);
object1<<ch;
}
object.close(); //member function of ofstream class
object1.close();

return 0;

}
17 changes: 17 additions & 0 deletions File_handling/eof_single_char_read.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream object;
char ch;
object.open("sameer.txt"); // member function of ofstream class
while(!object.eof())
{
object.get(ch);
cout<<ch;
}
object.close(); //member function of ofstream class
return 0;

}
12 changes: 12 additions & 0 deletions File_handling/file-2-file_modes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream object;
object.open("sameer.txt",ios::app); // file modes
object<<"hello world";
object.close(); //member function of ofstream class
return 0;

}
12 changes: 12 additions & 0 deletions File_handling/file1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream object;
object.open("sameer.txt"); // member function of ofstream class
object<<"hello world";
object.close(); //member function of ofstream class
return 0;

}
14 changes: 14 additions & 0 deletions File_handling/file_read_text_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream object;
char str[900];
object.open("sameer.txt"); // member function of ofstream class
object>>str;
cout<<str;
object.close(); //member function of ofstream class
return 0;

}
16 changes: 16 additions & 0 deletions File_handling/file_text_getline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream object;
char str[900];
object.open("sameer.txt"); // member function of ofstream class
object.getline(str,900); // read a line
cout<<str;
object.getline(str,900); // read a line
cout<<str;
object.close(); //member function of ofstream class
return 0;

}
24 changes: 24 additions & 0 deletions File_handling/multi+spaces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// program to remove multiple spaces from the text file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream object;
char ch;
int count =0;
object.open("sameer.txt"); // member function of ofstream class
while(!object.eof())
{
object.get(ch);
if(ch==' ')
count++;
else
count=0;
if(count<=1)
cout<<ch;
}
object.close(); //member function of ofstream class
return 0;

}
9 changes: 9 additions & 0 deletions File_handling/sameer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This my first attempt in c++
hello world
thiis is my third line
and fourth line
and fifth line
and
so
on
this ia again
18 changes: 18 additions & 0 deletions array/array-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* array declaration + initialization method
*/

#include<iostream>
#include<stdlib.h>
#include<iomanip>

#define random(num) rand()%num

using namespace std;
int main(){
int a[10]={10,20,30,40,60};

// output phase
for(int i=0;i<10;i++)
cout<<setw(10)<<a[i];
return 0;
}
30 changes: 30 additions & 0 deletions array/array-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* array declaration + initialization from keyboard
*/

#include<iostream>
#include<stdlib.h>
#include<iomanip>

using namespace std;
int main(){
int a[10];
int i;
//input phase
for(i=0;i<10;i++)
{
cout<<"\n Enter a["<<i+1<<"] number ";
cin>>a[i];
}

//processing phase
int sum =0;
for(i=0;i<10;i++)
sum = sum+a[i];

// output phase
cout<<"\n Entered array :";
for(i=0;i<10;i++)
cout<<setw(10)<<a[i];
cout<<"\n Sum of all elements :"<<sum;
return 0;
}
5 changes: 3 additions & 2 deletions array/array.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include<iostream>
#include<stdlib.h>
#include<iomanip>

#define random(num) rand()%100
#define random(num) rand()%num

using namespace std;
int main(){
int a[10],i;
//input phase
for(i=0;i<10;i++)
a[i] = random(100);
a[i] = random(10);
// output phase
for(i=0;i<10;i++)
cout<<setw(10)<<a[i];
Expand Down
38 changes: 38 additions & 0 deletions array/fun_array-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* array declaration + initialization from keyboard
*/

#include<iostream>
#include<stdlib.h>
#include<iomanip>

using namespace std;

int fun_add(int a){
a = a+10;
return(a);
}

int main(){
int a[10];
int i;

//input phase
for(i=0;i<10;i++)
{
cout<<"\n Enter a["<<i+1<<"] number ";
cin>>a[i];
}

//processing phase

for(i=0;i<10;i++)
a[i] = fun_add(a[i]);

// output phase

cout<<"\n Modified array :";
for(i=0;i<10;i++)
cout<<setw(10)<<a[i];

return 0;
}
39 changes: 39 additions & 0 deletions array/fun_array-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* array declaration + initialization from keyboard
*/

#include<iostream>
#include<stdlib.h>
#include<iomanip>

using namespace std;

void fun_add(int a[10]){
int i;
for(i=0;i<10;i++)
a[i] = a[i]+10;

}

int main(){
int a[10];
int i;

//input phase
for(i=0;i<10;i++)
{
cout<<"\n Enter a["<<i+1<<"] number ";
cin>>a[i];
}

//processing phase

fun_add(a);

// output phase

cout<<"\n Modified array :";
for(i=0;i<10;i++)
cout<<setw(10)<<a[i];

return 0;
}
32 changes: 32 additions & 0 deletions array/increment_by_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* array declaration + initialization from keyboard
*/

#include<iostream>
#include<stdlib.h>
#include<iomanip>

using namespace std;
int main(){
int a[10];
int i;
//input phase
for(i=0;i<10;i++)
{
cout<<"\n Enter a["<<i+1<<"] number ";
cin>>a[i];
}

//processing phase

for(i=0;i<10;i++)
a[i]+=10; //shortand operator a[i] = a[i]+10


// output phase

cout<<"\n Modified array :";
for(i=0;i<10;i++)
cout<<setw(10)<<a[i];

return 0;
}
30 changes: 30 additions & 0 deletions array/sum_element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* array declaration + initialization from keyboard
*/

#include<iostream>
#include<stdlib.h>
#include<iomanip>

using namespace std;
int main(){
int a[10];
int i;
//input phase
for(i=0;i<10;i++)
{
cout<<"\n Enter a["<<i+1<<"] number ";
cin>>a[i];
}

//processing phase
int sum =0;
for(i=0;i<10;i++)
sum = sum+a[i];

// output phase
cout<<"\n Entered array :";
for(i=0;i<10;i++)
cout<<setw(10)<<a[i];
cout<<"\n Sum of all elements :"<<sum;
return 0;
}

0 comments on commit cb59491

Please sign in to comment.