Sie sind auf Seite 1von 18

CSC138

CSC
CSC 128
128

Fundamental OF
FUNDAMENTAL Of COMPUTER
Computer
Problem Solving
PROBLEM SOLVING

PAST EXAM
ASSIGNMENT
PAST YEAR

BY: Ahmad Adam bin Adnan


By: Muhammad Nizam bin Yusuf
GROUP : CS110ID Num: 2015506461
Student
LECTURER
Date : 2:ndKhai!nnisa
March 2016
" Abdul Kadir

QUESTION
QUESTION 11
#include <iostream>
using namespace std;
int main()
{
const int size=10;
int sales[size] = {4,2,5,4,3,5,7,6,4,1};
int num, index, sum=0, oddNum=0, count4=0, count=0, highest=-999;
double avg;
double sumOdd=0;

for(int i=0; i<size; i++)


{ sum+= sales[i]; }

for(int k=0; k<size; k++)


{ if(sales[k]==4)
{ count4++; } }
cout<<"Please enter your number : ";
cin>>num;

for(int q=0; q<size; q++)


{ if(sales[q]==num)
{ count++; } }

for(int m=0;m<size;m++)
{ if(sales[m]>highest)
{ highest=sales[m];
index=m;} }

for(int t=0;t<size;t++)
{ if(sales[t]%2!=0)
{ oddNum++;} }

for(int s=0;s<size;s++)
{ if(sales[s]%2!=0)
{ sumOdd+=sales[s]; } }
avg=sumOdd/10;
cout<<"Item that is lower than average : ";

for(int h=0;h<size;h++)

2



{ if(sales[h]<avg)
{ cout<<"\t"<<sales[h]; } }

cout<<"\nSum of all items : "<<sum<<endl;


cout<<"The amount of number '4' in this array : "<<count4<<endl;
cout<<"The number in this array : "<<count<<endl;
cout<<"The highest value is : "<<highest<<endl;
cout<<"Index is : "<<index<<endl;
cout<<"The total of all odd numbers are : "<<oddNum<<endl;
cout<<"The sum of all odd numbers are : "<<sumOdd<<endl;
cout<<"The average value of odd numbers : "<<avg<<endl;
}

3



QUESTION 22
QUESTION
#include <iostream>
using namespace std;
int main()
{
int array[30],total=0,count20=0;
for(int h=0;h<30;h++)
{ cout<<"Enter a number (30 even number) : ";
cin>>array[h]; }

for(int i=0;i<30;i++)
{ total+=array[i]; }

for(int j=0;j<30;j++)
{ if(array[j]==20)
count20++; }
cout<<"The total of all array summation :"<<total<<endl;
cout<<"The amount of 20 : "<<count20<<endl;
}

4



QUESTION 33
QUESTION
#include <iostream>
using namespace std;
int main()
{
double marks[7], hold, large=-999;
cout<<"Please enter your marks : ";
for(int i=0;i<7;i++)
{ cin>>marks[i]; }
cout<<endl;

for(int q=0; q<7; q++)


{ cout<<marks[q]<<endl; }

for(int r=0; r<7; r++)


{ if(marks[r]>large)
{ large=marks[r]; } }
cout<<"The highest mark is : "<<large<<endl;

for(int i=1;i<7;i++)
{ for(int j=0;j<(7-i);j++)
{if(marks[j]>marks[j+1])
{ hold=marks[j+1];
marks[j+1]=marks[j];
marks[j]=hold; } } }

for(int i=0;i<7;i++)
{ cout<<marks[i]<<" "; }
cout<<endl<<endl; }

5



QUESTION 44
QUESTION
#include <iostream>
using namespace std;
void findNumOfStudents(double arr[],int size);
double findAverage(double arr[],int size);
double findHighest(double arr[],int size);
int searchScore(double arr[],int size,int search);
void sortScore(double arr[],int size);
int main()
{
int size=10;
double score[10]={55.0,67.0,52.5,70.5,82.0,31.0,88.0,64.5,55.0,89.0};
double average,highest;
int location,search;
findNumOfStudents(score,10);

average=findAverage(score,10);
cout<<"Average score is : "<<average<<endl;

highest=findHighest(score,10);
cout<<"Highest score is : "<<highest<<endl;

cout<<"Enter your search score : ";


cin>>search;
location=searchScore(score,size,search);
cout<<"The location for the search score is : "<<location<<endl;
sortScore(score,10); }

void findNumOfStudents(double score[],int size)


{
int getA=0,getB=0,getC=0,getD=0,getF=0;
for(int i=0;i<size;i++)
{ if(score[i]<=100 && score[i]>=90)
{ getA++; }

if(score[i]<=89 && score[i]>=80)


{ getB++; }

if(score[i]<=79 && score[i]>=70)


{ getC++; }

if(score[i]<=69 && score[i]>=60)


{ getD++; }

6



if(score[i]<=59 && score[i]>=0)
{ getF++; } }
cout<<"Student that get A is : "<<getA<<endl;
cout<<"Student that get B is : "<<getB<<endl;
cout<<"Student that get C is : "<<getC<<endl;
cout<<"Student that get D is : "<<getD<<endl;
cout<<"Student that get F is : "<<getF<<endl; }

double findAverage(double score[],int size)


{
double sum=0,avg=0;
for(int i=0;i<size;i++)
{ sum=sum+score[i]; }
avg=sum/size;
return avg; }

double findHighest(double score[],int size)


{
double max=-999;
for(int i=0;i<size;i++)
{ if(max<score[i])
{ max=score[i]; } }
return max; }

int searchScore(double score[],int size,int search)


{
int locate=-1;
for(int i=0;i<size;i++)
{ if(score[i]==search)
{ locate=i; } }
return locate; }

void sortScore(double score[],int size)


{
double hold=0;
cout<<"\nThe score in ascending orders : "<<endl;
for(int i=1;i<size;i++)
{ for(int j=0;j<(size-i);j++)
{ if(score[j]>score[j+1])
{ hold=score[j+1];
score[j+1]=score[j];
score[j]=hold; } } }

for(int i=0;i<size;i++)
{ cout<<score[i]<<" "; }
cout<<endl; }

7



8



QUESTION
QUESTION55
#include<iostream>
using namespace std;
void insertMark(double arr[],int size);
double calcTotal(double arr[],int size);
int main()
{
int size=5;
double marks[5];
double total=0;
insertMark(marks,size);
total=calcTotal(marks,size);
cout<<"\nThe total marks is : "<<total<<endl;
}

void insertMark(double marks[],int size)


{
cout<<"Enter your mark for 5 subjects : "<<endl;
for(int i=0;i<size;i++)
{cin>>marks[i]; }
}

double calcTotal(double marks[],int size)


{
double totalAll=0;
cout<<"\nMarks is : ";
for(int i=0;i<size;i++)
{ cout<<marks[i]<<" ";
totalAll=totalAll+marks[i]; }
return totalAll; }

9



QUESTION
QUESTION 66
#include<iostream>
using namespace std;
int main()
{
float money[7];
float total=0, highest=-999;
int day=1;
for(int i=0;i<7;i++)
{ cout<<"Enter amount of payment for Day "<<day<<" : ";
cin>>money[i];
day++; }

for(int c=0;c<7;c++)
{ if(money[c]>highest)
{ highest=money[c]; } }
cout<<"\nThe highest total payment received by the Clinic is : "<<highest<<endl;
}

10



QUESTION 1

#include<iostream>
using namespace std;
struct carrymark

{ char name[100];
int id;
float test1;
float test2;
float quiz;
float assignment;
float total_mark; };

int main()
{ carrymark studentmark;
cout<<"Insert student name:";
cin.getline(studentmark.name,100);
cout<<"insert student id:";
cin>>studentmark.id;
cout<<"insert test 1:";
cin>>studentmark.test1;
cout<<"insert test 2:";
cin>>studentmark.test2;
cout<<"insert quiz mark:";
cin>>studentmark.quiz;
cout<<"insert assignment mark:";
cin>>studentmark.assignment;

studentmark.total_mark=(studentmark.test1/0.5*0.1)+(studentmark.test2/0.5*0.1)+(stu
dentmark.quiz/0.5*0.1)+(studentmark.assignment/0.5*0.1);
cout<<"student name : "<<studentmark.name<<endl;
cout<<"student id : "<<studentmark.id<<endl;
cout<<"test 1 mark : "<<studentmark.test1<<endl;
cout<<"test 2 mark : "<<studentmark.test2<<endl;
cout<<"Quiz mark : "<<studentmark.quiz<<endl;
cout<<"assignment mark : "<<studentmark.assignment<<endl;
cout<<"total carry mark : "<<studentmark.total_mark<<endl; }

1


1
QUESTION 1

2


2
QUESTION 2

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main()
{ ifstream infile;
ofstream outfile1;
ofstream outfile2;
infile.open("itemInfo.txt");
outfile1.open("Computer.txt");
outfile2.open("Others.txt");

char code[40];
char type[40];
char descp[40];
double price;

if(!infile)
{ cout<<"Error: File Doesn't Exist!'"<<endl; }
else
{ coutfile1<<" COMPUTER "<<endl;
coutfile2<<" OTHERS "<<endl; }
while(!infile.eof())
{ infile.getline(code,40,';');
infile.getline(type,40,';');
infile.getline(descp,40,';');
infile>>price;
if(strcmp(type,"COMPUTER")==0)
outfile1<<code<<" "<<descp<<" "<<price<<endl;

else
outfile2<<code<<" "<<descp<<" "<<price<<endl; }
cout<<"Processing Data"<<endl; }
infile.close();
outfile1.close();
outfile2.close(); }

3


3
QUESTION 2

4


4

COMPUTER

OTHERS
5


5
QUESTION 3

#include <iostream>
#include <fstream>
using namespace std;
int main()
{ ifstream infile;
ofstream outfile1;
ofstream outfile2;

infile.open("SUKSIS.txt");
outfile1.open("PASS.txt");
outfile2.open("FAIL.txt");
char name[50];
char gender;
int weight;
double height,BMI;

if (!infile)
{ cout<<"NO File IS Found~!"; }
else
{ while(!infile.eof())
{ infile.getline(name,50,'#');
infile>>gender;
infile>>weight;
infile>>height;
BMI=(weight/(height*height));

if(gender=='M')
{
if(weight>=50)
{
if(height>=1.63)
{
if(BMI>=18.0 && BMI<=23.0)
{
outfile1<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl;}
else

6


6
outfile2<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl; }
else
outfile2<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl; }
else
outfile2<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl; }
else
if(gender=='F')
{ if(weight>=48)
{ if(height>=1.57)
{ if(BMI>=18.0 && BMI<=23.0)
{
outfile1<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl;}
else

outfile2<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl; }


else

outfile2<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl; }


else

outfile2<<name<<" "<<gender<<" "<<weight<<" "<<height<<endl; } } }


cout<<"Processing Data!<< endl;
infile.close();
outfile1.close();
outfile2.close(); }

7


7
QUESTION 3

8


8

Das könnte Ihnen auch gefallen