Practice Make Perfect!

Assg 2

Posted by: dzikrina on: October 15, 2008

#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>

using namespace std;

int size;

int Random(int n)
{
    srand(time(NULL));
    return ((rand()*10000)%(size-1));
}

int main()
{
    ofstream original("string.txt");
    ofstream shuffleOut("shuffle.txt");

    cout << "Begin file creation\n";

 //   original.open
  //  shuffleOut.open("shuffle.txt");

    cout<<"Insert how many words"<<endl;
    cin>>size;

    int mynum[size];
    string word[size];

    cout<<"Please insert the words that you want to scramble"<<endl;

    for(int p=0;p<size;p++)
    cin>>word[p];

    cout<<endl;  

    cout<<"Original word...."<<endl;
    for(int q=0;q<size;q++)
    {
    cout<<word[q]<<" ";
    original<<word[q]<<" ";

    }

 char answer='n';
 int iteration=0;

 do
 {
            cout<<endl
                <<"Scrambling character(s)..."<<endl;

           iteration++;
           for (int i=size;i>=0;i--)
           {    

            std::random_shuffle(word,word+(size),Random);
           }            

            cout<<endl;

            cout<<"Iteration #"<<iteration<<endl;

            for(int m=0;m<size;m++)
            {
                cout<<word[m]<<" ";
                shuffleOut<<word[m]<<" ";

            }

                cout<<endl;

                cout<<"Reshuffle?"<<endl;
                cin>>answer;

            if(answer=='n'||answer=='N')
            {
               cout<<"Stop at iteration #"<<iteration<<endl;
               cout<<"Thank you!"<<endl;
 //              original.close("string.txt");
  //             shuffleOut.close("shuffle.txt");
            } 

            shuffleOut<<'\n';
  }while(answer=='y'||answer=='Y');

    cout<<endl;

    system("pause");
    return 0;
}

Array 2

Posted by: dzikrina on: September 11, 2008

/* Array, insert marks for x quizes for two subjects
find out how many marks in particular subject greater than the other
*/
# include <iostream>
using namespace std;
int main ()
{
 int x,i;
 int prog [x];
 int math [x];
 
 int qprog=0;
 int qmath=0;
 
 cout << “How many quiz that you want to evaluate?” << endl;
 cin >> x;
 
 cout << “Insert marks for math” << endl;
 for (int i=0; i<x; i++)
 {
  cin >> math [x];
 }
 
 cout << “Insert marks for programming” << endl;
 for (int i=0; i<x; i++)
 {
  cin >> prog [x];
 }
 
 for (i=0; i < x; i++)
  {
   if (prog [x] > math [x])
   qprog ++;
   else if (prog [x] < math [x])
   qmath++;
   else;
  }
 cout << qmath<< endl;
 cout << qprog<< endl;
 if (qmath > qprog)
  cout << “Your score better on math for ” << qmath << “number of quiz(es)”<< endl;
  
 else if (qmath < qprog)
  cout << “Your score  better on programming quiz ” << qprog << “number of quiz(es)” << endl;
  
 else
  cout << “Same” << endl;
  
 system (“pause”);
 return 0;
 
}

Linear Array

Posted by: dzikrina on: September 11, 2008

/*Linear Array
*/
# include <iostream>
using namespace std;
int main ()

{
 int i;
 int progquiz[5] = {40, 50, 60, 70, 100};
 int mathquiz [5] = {60, 80, 90, 80, 30};
 
 int qprogquiz=0;
 int qmathquiz=0;
 
 for (i=0; i<5; i++)
  { if (progquiz [i] > mathquiz [i])
   qprogquiz++;
   else
   qmathquiz++;
  }
 if (qmathquiz>qprogquiz)
  cout << “You score better on math for ” << qmathquiz << ” number of quiz(es)” << endl;
 else
 cout << “You score better on programming for” << qprogquiz;
 
  
 system (“pause”);
 return 0;
 }

TEXT IO part 3

Posted by: dzikrina on: September 10, 2008

/*create a program that can open the file
*/
# include <iostream>
# include <fstream>
# include <cstdlib>

using namespace std;
int main ()
{
string s;
ifstream fin;

fin.open (“tutor.txt”); // input file tutor.txt

if (!fin)
{
cerr << “error open file”;
exit (100);
}
getline(fin,s);
cout << s;

system (“pause”);
return 0;
}

TEXT IO part 2

Posted by: dzikrina on: September 10, 2008

/*create a program thet can store the user input until . to terminate
into file tutor.txt
*/
# include <iostream>
# include <fstream>
# include <cstdlib>

using namespace std;
int main ()
{   string text;
ofstream fout;

fout.open (“tutor.txt”); // create file tutor.txt
cout << “enter the sentence and . to terminate”<< endl;
getline(cin, text, ‘.’);

if (!fout)
{
cerr << “error open file”;
exit (100);
}
fout <<text;

system (“pause”);
return 0;
}

TEXT IO part 1

Posted by: dzikrina on: September 10, 2008

/*create a program thet can store text “halo”
into file tutor.txt
*/
# include <iostream>
# include <fstream>
# include <cstdlib>

using namespace std;
int main ()
{
ofstream fout;
fout.open (“tutor.txt”); // create file tutor.txt
if (!fout)
{
cerr << “error open file”;
exit (100);
}
fout<<”halo”;
system (“pause”);
return 0;
}

repetition 2

Posted by: dzikrina on: August 25, 2008

/*repetiton 2 25/08/08*/

# include <iostream>
using namespace std;
void getMenu (int& menu);
void decrementFor (int& j);
void decrementWhile (int& j);
void decrementDoWhile (int& j);
void nestedLooping (int& j);

int  main ()
{
int j,  menu;
cout << “Please enter a number : ” << endl;
cin >> j;

# include <iostream>
using namespace std;
void getMenu (int& menu);
void decrementFor (int& j);
void decrementWhile (int& j);
void decrementDoWhile (int& j);
void nestedLooping (int& j);

int  main ()
{
int j,  menu;
cout << “Please enter a number : ” << endl;
cin >> j;

getMenu (menu);
if (menu == 1)
decrementFor (j);
else if (menu == 2)
decrementWhile (j);
else if (menu == 3)
decrementDoWhile (j);
else if (menu == 4)
nestedLooping (j);
else
cout << “unexpected” << endl;

system (“pause”);
return 0;
}
void getMenu (int& menu)
{
int i;
cout << “—MENU—” << endl;
cout << “1 = decrement using FOR”<<endl;;
cout << “2 = decrement using WHILE”<<endl;
cout << “3 = decrement using DO WHILE”<<endl;
cout << “4= nested looping” << endl;

cin >> menu;
return;
}
void decrementFor (int& j)
{
int i;
for (i =j; i > 0; i–)
{
cout << “your number is : “<<i<< endl;
}
return;
}
void decrementWhile(int& j)
{
int i = j;
while (i > 0)
{ cout << “Your number is: ” << i << endl;
i –;
}
return;
}
void decrementDoWhile (int& j)
{
int i = j;
do
{
cout << “your number is : ” << i<<endl;
i–;
}
while (i > 0);

return;
}
void nestedLooping (int& j)
{
int i=j;
for (i= j; i > 0; i–)
{
for (i >0; i = j; i–)
{
cout <<i;
}
cout << endl;
}
return;
}
getMenu (menu);

if (menu == 1)
decrementFor (j);
else if (menu == 2)
decrementWhile (j);
else if (menu == 3)
decrementDoWhile (j);
else if (menu == 4)
nestedLooping (j);
else
cout << “unexpected” << endl;

system (“pause”);
return 0;
}
void getMenu (int& menu)
{
int i;
cout << “—MENU—” << endl;
cout << “1 = decrement using FOR”<<endl;;
cout << “2 = decrement using WHILE”<<endl;
cout << “3 = decrement using DO WHILE”<<endl;
cout << “4= nested looping” << endl;

cin >> menu;
return;
}
void decrementFor (int& j)
{
int i;
for (i =j; i > 0; i–)
{
cout << “your number is : “<<i<< endl;
}
return;
}
void decrementWhile(int& j)
{
int i = j;
while (i > 0)
{ cout << “Your number is: ” << i << endl;
i –;
}
return;
}
void decrementDoWhile (int& j)
{
int i = j;
do
{
cout << “your number is : ” << i<<endl;
i–;
}
while (i > 0);

return;
}
void nestedLooping (int& j)
{
int i=j;
for (i= j; i > 0; i–)
{
for (i >0; i = j; i–)
{
cout <<i;
}
cout << endl;
}
return;
}

Repetition 1

Posted by: dzikrina on: August 19, 2008

/* repetition using for
19/08/08*/

# include <iostream>
using namespace std;
int main ()
{
char grade;
cout << “Enter grades” << endl;
for (int i=1; i <= 10; i++)
cin >> grade;

system (“pause”);
return 0;
}

Hijriyah callendar using function

Posted by: dzikrina on: August 13, 2008

/*USING VARIABLE*/
# include <iostream>
# include <string>
using namespace std;

string numToString (int num);
int main ()
{    int num;
string month;
month = numToString (num);
cout << “Enter a month” << endl;
cin >> num;
cout << num << “Month is ” <<month;

system (“pause”);
return 0;

}
string numToString (int num)
{ string month;
if (num == 1)
month = “Muharram”;

else if (num == 2)
month = “Safar”;

else if (num == 3)
month = “Rabiul Awal”;

else if (num == 4)
month = “Rabiul Tsani”;

else if (num == 5)
month = “Jumadil Awal”;

else if (num == 6)
month = “Jumadil Tsani”;

else if (num == 7)
month = “Rajab”;

else if (num == 8)
month = “Syaban”;

else if (num == 9)
month = “Ramadhan”;

else if (num == 10)
month = “Syawal”;

else if (num == 11)
month = “Dzulqoidah”;

else if (num==12)
month = “Dzulhijjah”;

else
month = “out of range”;

return (month);
}

Hijriyah callendar

Posted by: dzikrina on: August 13, 2008

/*Design and implement an application that read an integer value from the key board
and convert it to hijriyah month*/
# include <iostream>
using namespace std;
int main ()
{
int num;
char month;
cout << “Enter a month” << endl;
cin >> num;
if (num == 1)
cout << “The month is Muharram” << endl;
else if (num == 2)
cout << “The month is Safar” << endl;
else if (num == 3)
cout << “The month is Rabiul Awal” << endl;
else if (num == 4)
cout << “The month is Rabiul Tsani” << endl;
else if (num == 5)
cout << “The month is Jumadil Awal” << endl;
else if (num == 6)
cout << “The month is Jumadil Tsani” << endl;
else if (num == 7)
cout << “The month is Rajab” << endl;
else if (num == 8)
cout << “The month is Syaban” << endl;
else if (num == 9)
cout << “The month is Ramadhan” << endl;
else if (num == 10)
cout << “The month is Syawal” << endl;
else if (num == 11)
cout << “The month is Dzulqoidah” << endl;
else if (num==12)
cout << “The month is Dzulhijjah” <<endl;
else
cout << “Out of range” <<endl;

system (“pause”);
return 0;
}

 

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Top Clicks

  • None