0% found this document useful (0 votes)
30 views

Assignment 6

This document contains the source code for 3 questions on an assignment about arrays in C++. Question 1 defines functions to input scores into an array, calculate the average of the scores, and display the scores. Question 2 defines an array to input and sort numbers in ascending and descending order. Question 3 defines an array to input numbers and display only the distinct numbers. The document provides the source code assignments were to complete on arrays for a computer programming course.

Uploaded by

nana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Assignment 6

This document contains the source code for 3 questions on an assignment about arrays in C++. Question 1 defines functions to input scores into an array, calculate the average of the scores, and display the scores. Question 2 defines an array to input and sort numbers in ascending and descending order. Question 3 defines an array to input numbers and display only the distinct numbers. The document provides the source code assignments were to complete on arrays for a computer programming course.

Uploaded by

nana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIVERSITI PUTRA MALAYSIA

FAKULTI KEJURUTERAAN
Faculty of Engineering

FACULTY OF ENGINEERING

DEPARTMENT OF CIVIL ENGINEERING

ECC 3005 C0MPUTER PROGRAMMING

SEMESTER 1 2018/2019

TITLE : ASSIGNMENT 6 (ARRAY)

NAME: NURJANNAH BINTI AMIR

MATRIC NUMBER: 193233

NAME OF LECTURER: DR. AIDI HIZAMI ALES @ ALIAS

DATE OF SUBMISSION: 30 NOVEMBER 2018


QUESTION1

#include <iostream>
using namespace std;
const int maXsize = 10;

int input(int scores[], int arrsize);


double avg(int scores[], int arrsize);
void display(int scores[], int arrsize);

int input(int scores[], int arrsize)


{
double score;
int count = 0;
cout <<"Enter 10 scores \n";
for(int i = 0; i < maXsize; i++)
{
cout <<"Enter Score "<< (i + 1) << " : ";
cin >> score;
if(score > 0)
{
scores[i] = score;
count++;
}
else
break;

}
return count;

double avg(int scores[], int arrsize)

{
double total = 0;
for(int i = 0; i < arrsize; i++)
{
total += scores[i];
}
return total/arrsize;
}

int main()
{
int scores[maXsize];
int arrsize = input(scores, maXsize);
display(scores, arrsize);
cout <<" Average scores:\n "<< avg(scores, arrsize);
return 0;
}

void display(int scores[], int arrsize)


{
cout << "Scores:";
for(int i = 0; i < arrsize; i++)
{
cout << scores[i] << " ";
}

}
QUESTION2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n[10],f,g,x;
int sum;
cout<<"Enter ten numbers "<<endl;
cout<<" "<<endl;

for(int f = 0; f<10; f++)


{
cin>>n[f];
}

for ( f= 0; f<10; ++f )


{
for ( g= f+1; g<10; ++g)
{
if (n[f]>n[g])
{
x= n[f];
n[f] = n[g];
n[g] = x;
}
}
}
cout<<endl;

cout<<"Ascending order : "<<endl;


for (f=0; f<10; ++f)
cout<< n[f] << " ";
cout<<endl;

cout<<endl;

cout<<"Descending order :"<<endl;


for (f=10-1; f>=0; f--)
cout<< n[f] << " ";
cout<<endl;

system ("PAUSE");
return 0;
}
QUESTION3

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, k;
int num[10];
cout<<"Enter ten numbers "<<endl;
cout<<""<<endl;

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


{
cin>> num[i];
}

cout<<"The distinct numbers are : "<<endl;

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


{
int k;
for(k=0; k<i; k++)
if (num[i] == num [k])
break;

if (i==k)
{
cout<< num[i]<<" ";
}
}

return 0;
}

You might also like