#include <stdio.h>
void evenCalc(int arr[][10],int n);
void oddCalc(int arr[][10],int n);
int main(){
int arr[3][10] = {{38,97,25,66,90,31,16,86,33,59},{21,14,36,39,92,11,18,48,25,49},{33,44,29,46,45,66,56,91,27,65}};
int rowsize = sizeof(arr)/sizeof(arr[0]);
printf("[실행결과]\n");
printf("********************\n");
evenCalc(arr,rowsize);
oddCalc(arr,rowsize);
printf("********************\n");
return 0;
}
void evenCalc(int arr[][10],int n)
{
int i,j;
int total = 0;
int cnt = 0;
double avg = 0;
for(i=0;i<n;i++)
{
for(j=0;j<10;j++)
{
if(arr[i][j]%2==0)
{
total+=arr[i][j];
cnt++;
}
}
}
avg = (double)total/cnt;
printf("짝수의 합 : %d\n",total);
printf("짝수의 평균 : %.3lf\n",avg);
}
void oddCalc(int arr[][10],int n)
{
int i,j;
int total = 0;
int cnt = 0;
double avg = 0;
for(i=0;i<n;i++)
{
for(j=0;j<10;j++)
{
if(arr[i][j]%2!=0)
{
total+=arr[i][j];
cnt++;
}
}
}
avg = (double)total/cnt;
printf("홀수의 합 : %d\n",total);
printf("홀수의 평균 : %.3lf\n",avg);
}
2017년 5월 31일 수요일
2017년 5월 25일 목요일
0525 수업 내용 : 배열탐색_선형/이진탐색
/*선형 탐색 프로그램*/
#include <iostream>
using namespace std;
int lineSearch(const int list[],int key,int size);
int main(){
int const SIZE = 6;
int list[] = {6,4,1,9,7,3,2,8};
int i = lineSearch(list,3,SIZE);
cout<<i<<endl;
return 0;
}
int lineSearch(const int list[],int key,int size)
{
for(int i=0;i<size;i++)
{
if(key==list[i])
return i;
}
return -1;
}
/*이진 탐색 프로그램*/
#include <iostream>
using namespace std;
int binarySearch(const int list[],int key,int arraySize);
int main(){
int list[] = {2,4,7,10,15,16,19,20,27,56,59,80};
int i = binarySearch(list,80,20);
cout<<i<<endl;
return 0;
}
int binarySearch(const int list[],int key,int arraySize)
{
int low = 0;
int high = arraySize-1;
while(high>=low)
{
int mid = (low+high)/2;
if(key < list[mid])
high = mid - 1;
else if(key == list[mid])
return mid;
else
low = mid + 1;
}
return low - 1;
}
#include <iostream>
using namespace std;
int lineSearch(const int list[],int key,int size);
int main(){
int const SIZE = 6;
int list[] = {6,4,1,9,7,3,2,8};
int i = lineSearch(list,3,SIZE);
cout<<i<<endl;
return 0;
}
int lineSearch(const int list[],int key,int size)
{
for(int i=0;i<size;i++)
{
if(key==list[i])
return i;
}
return -1;
}
/*이진 탐색 프로그램*/
#include <iostream>
using namespace std;
int binarySearch(const int list[],int key,int arraySize);
int main(){
int list[] = {2,4,7,10,15,16,19,20,27,56,59,80};
int i = binarySearch(list,80,20);
cout<<i<<endl;
return 0;
}
int binarySearch(const int list[],int key,int arraySize)
{
int low = 0;
int high = arraySize-1;
while(high>=low)
{
int mid = (low+high)/2;
if(key < list[mid])
high = mid - 1;
else if(key == list[mid])
return mid;
else
low = mid + 1;
}
return low - 1;
}
0525 수업내용_배열 역순 복사
#include <iostream>
using namespace std;
void reverse(const int list[],int newList[],int size);
void printArray(const int list[],int size);\
int main(){
const int SIZE = 6;
int list[] = {1,2,3,4,5,6};
int newList[SIZE] = {0};
reverse(list,newList,SIZE);
cout<<"Origianl Array : ";
printArray(list,SIZE);
cout<<"\n";
cout<<"The reversed Array : ";
printArray(newList,SIZE);
cout<<"\n";
return 0;
}
void reverse(const int list[],int newList[],int size)
{
for(int i = 0,j=size-1;i<size;i++,j--)
{
newList[j] = list[i];
}
}
void printArray(const int list[],int size){
for(int i=0;i<size;i++)
cout<<list[i]<<" ";
}
using namespace std;
void reverse(const int list[],int newList[],int size);
void printArray(const int list[],int size);\
int main(){
const int SIZE = 6;
int list[] = {1,2,3,4,5,6};
int newList[SIZE] = {0};
reverse(list,newList,SIZE);
cout<<"Origianl Array : ";
printArray(list,SIZE);
cout<<"\n";
cout<<"The reversed Array : ";
printArray(newList,SIZE);
cout<<"\n";
return 0;
}
void reverse(const int list[],int newList[],int size)
{
for(int i = 0,j=size-1;i<size;i++,j--)
{
newList[j] = list[i];
}
}
void printArray(const int list[],int size){
for(int i=0;i<size;i++)
cout<<list[i]<<" ";
}
피드 구독하기:
글 (Atom)
