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

Lab 5

This document contains 6 C programs that perform various tasks on arrays: 1. Calculates the average, mode, and standard deviation of an array 2. Determines the cumulative frequency distribution of student exam scores in an array 3. Checks if a number or any following number is both prime and a palindrome 4. Checks for matching elements between two arrays 5. Moves all zero elements in an array to the end 6. Prints odd then even elements of an array, printing "None" if the set is empty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lab 5

This document contains 6 C programs that perform various tasks on arrays: 1. Calculates the average, mode, and standard deviation of an array 2. Determines the cumulative frequency distribution of student exam scores in an array 3. Checks if a number or any following number is both prime and a palindrome 4. Checks for matching elements between two arrays 5. Moves all zero elements in an array to the end 6. Prints odd then even elements of an array, printing "None" if the set is empty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab5: Array Grader

//ID: 66070503459 Anongnart Boonkleang


//ID: 66070503459 Anongnart Boonkleang
//EXAM1 : Statistics (Average, Mode, SD)
#include<stdio.h>
#include<math.h>

int main() {
int i,j,n,k=0, arr[1000],mode[1000]={0},count[1000]={0};
double x=0.00,y=0.00;

scanf("%d", &n);
if(n>1000 && n<1){
return 0;
}

for(i=0; i<n ;i++){


scanf("%d", &arr[i]);
if(arr[i] > 1000 & arr[i]<-1000){
return 0;
}
x = x + arr[i];
}
x = x/n;
printf("%.2lf\n",x);

for(i=0 ; i<n ; i++){


for(j=i ; j<n ; j++){
if(arr[i] == arr[j]){
mode[i]++;
}
}
}
for(i=0; i<n ; i++){
for(j=0 ; j<n ; j++){
if(mode[i] < mode[j] && mode[i]==1){
count[i]=1;
}
}
}
for(i=0; i<n ; i++){
if(count[i]==0){
k++;
}
}
if(k>1 && k<2){
printf("NONE\n");
}else{
for(i=0 ; i<n ; i++){
if(count[i] == 0){
printf("%d",arr[i]);
}
}
printf("\n");
}
for(i=0; i<n;i++){
y = y + (arr[i]-x)*(arr[i]-x);
}
printf("%.2lf\n", sqrt(y/n));
return 0;
}

//ID: 66070503459 Anongnart Boonkleang


//EXAM2 : Culmulative Frequency
#include<stdio.h>

int main() {
int i,n,Xi,Yi,Zi,arr[100][2],count[5]={0};
double score=0, percent;

scanf("%d", &n);
if(n>100 && n<1){
return 0;
}
for(i=0 ; i<n ; i++){
scanf("%d %d %d", &Xi, &Yi, &Zi);

if(Xi > 40 && Xi<-20 , Yi>40 && Yi<-20 , Zi>20 && Zi<-20){
return 0;
}
arr[i][0]= Xi+Yi+Zi;

if(arr[i][0]>=80){
arr[i][1] = 4;
}else if(arr[i][0] >= 70){
arr[i][1] = 3;
}else if(arr[i][0] >= 60){
arr[i][1] = 2;
}else if(arr[i][0] >= 50){
arr[i][1] = 1;
}else{
arr[i][1] = 0;
}
}
for(i=0 ; i<n ; i++){
switch(arr[i][1]){
case 4:
count[0]++;
break;
case 3:
count[1]++;
break;
case 2:
count[2]++;
break;
case 1:
count[3]++;
break;
default:
count[4]++;
}
}
for(i=0 ; i<5 ; i++){
score = score + count[i];
percent = (score/n) * 100;
printf("%.0lf %.2lf\n", score, percent);
}
return 0;
}
//Finish 100 score
//ID: 66070503459 Anongnart Boonkleang
//EX3: Prime Palindrome
#include<stdio.h>
#include<math.h>

int isPrime(int n){


if(n<=1){
return 0;
}
for(int i=2; i<n ; i++){
if(n%i == 0){
return 0; //False
}
}
return 1;
}

int isPalindrome(int n){


int ori = n, new=0,digit;
while(n>0){
digit = n%10;
new = new * 10 + digit;
n = n/10;
}
return ori == new;
}
int main() {
int i,j;
scanf("%d", &i);

if(i>10000 || i<0){
return 0;
}
j = i+1;
while(1){
if(isPrime(j) && isPalindrome(j)){
printf("%d", j);
break;
}
j++;
}
return 0;
}
//Finish 100 score
//ID: 66070503459 Anongnart Boonkleang
//EXAM4 : Matching
#include<stdio.h>
#include<stdbool.h>

int main() {
int n,m,i,j,setA[100],setB[100];
bool output = true;

scanf("%d",&n); //n represents the number of elements in set A


for(i=0 ; i<n ; i++){
scanf("%d", &setA[i]);
}

scanf("%d", &m); //m represents the number of elements in set B


for(i=0 ; i<m ; i++){
scanf("%d", &setB[i]);
}
for(i=0 ; i<m ; i++){
for(j=0 ; j<n ; j++){
if(setB[i] == setA[j]){
setA[j] = setB[i] = 9999;
}
}
}
for(i=0; i<m; i++){
if(setB[i] != 9999){
output = false;
break;
}
}
printf(output?"True":"False");
return 0;
}
//Finish 100 score
//ID: 66070503459 Anongnart Boonkleang
//EXAM5 : Move Zero
#include <stdio.h>

int main() {
int i,n,arr[100],count=0,Xi;
scanf("%d", &n);

if(n > 100 && n < 1){


return 0;
}

for(i=0 ; i<n ; i++){


scanf("%d", &Xi);
if(Xi > 1000 && Xi < -1000){
return 0;
}
if(Xi != 0){
arr[count++] = Xi;
}
}
while(count < n){
arr[count++] = 0;
}
for(i=0 ; i<n ; i++){
printf("%d\n", arr[i]);
}
return 0;
}
//Finish 100 score
//ID: 66070503459 Anongnart Boonkleang
//EXAM6 : Odd Even
#include<stdio.h>

int main(){
int i,n,arr[100],even=0,odd=0;
scanf("%d", &n);

for(i=0 ; i<n ; i++){


scanf("%d", &arr[i]);
if(arr[i] % 2 == 0){
even++;
}else {
odd++;
}
}
if(odd == 0){
printf("None\n");
}else{
for(i=0 ; i<n ; i++){
if(arr[i] % 2 != 0){
printf("%d ", arr[i]);
}
}
printf("\n");
}
if(even == 0){
printf("None\n");
}else{
for(i=0 ; i<n ; i++){
if(arr[i] % 2 == 0){
printf("%d ", arr[i]);
}
}
}
return 0;
}
//Finish 100 score

You might also like