skip to main |
skip to sidebar
#include
#include
#include
#define max 10
void print_array(int *array) {
int x;
for(x = 0; x if(x != max-1)
printf("%d, ", array[x]);
else
printf("%d\n", array[x]);
}
}
void main(){
int iarray[max];
int i,j;
int k;
// Seed rand()
srand((unsigned int)time(NULL));
for(i = 0; i < max; i++)
iarray[i] = (int)(rand() % 100);
// print initial data
printf("initial data \n");
print_array(iarray);
printf("\n");
//insertion sort
for(i=0 ; i k=iarray[i];
for(j=i-1;i>0 && k iarray[j+1] = iarray[j];
iarray[j] = k;
}
printf("round %d\n",i);
print_array(iarray);
}
printf("result sort\n");
print_array(iarray);
}
#include
#include
#include
#define max 10
void print_array(int *array) {
int x;
for(x = 0; x if(x != max-1)
printf("%d, ", array[x]);
else
printf("%d\n", array[x]);
}
}
void main(){
int iarray[max];
int i,j;
int temp;
int min;
// Seed rand()
srand((unsigned int)time(NULL));
for(i = 0; i < max; i++)
iarray[i] = (int)(rand() % 100);
//selection sort
for(i=0 ; i printf("round %d\n",i);
print_array(iarray);
min = i;
for(j=i+1;j if(iarray[j] min = j;
temp = iarray[i];
iarray[i] = iarray[min];
iarray[min]=temp;
}
printf("result sort\n");
print_array(iarray);
}
#include
#include
#include
#define max 10
void print_array(int *array) {
int x;
for(x = 0; x if(x != max-1)
printf("%d, ", array[x]);
else
printf("%d\n", array[x]);
}
}
void main(){
int iarray[max];
int x,y;
int temp;
// Seed rand()
srand((unsigned int)time(NULL));
for(x = 0; x < max; x++)
iarray[x] = (int)(rand() % 100);
//print_array(iarray);
printf("initial data\n");
print_array(iarray);
// Bubble sort method.
for(x = 0; x < max; x++){
for(y = 0; y < max-1; y++)
if(iarray[y] > iarray[y+1]) {
temp = iarray[y+1];
iarray[y+1] = iarray[y];
iarray[y] = temp;
}
printf("\n round %d\n",x);
print_array(iarray);
}
printf("\n result\n");
print_array(iarray);
}
#include
int sum(int n)
{
if(n<=0)
return 0;
else
n= n+sum(n-1);
return n;
}
void main(){
int n = 10;
int summ;
summ = sum(n);
printf("sum = %d\n",summ);
}
.........................
factorial
#include
int factorial(int n)
{
if(n<=0)
return 1;
else
n= n*factorial(n-1);
return n;
}
void main(){
int n = 5;
int summ;
summ = factorial(n);
printf("sum = %d\n",summ);
}
................................
max min!
#include
void minmax(int *listnumber,int n,int *p_min,int *p_max){
int min2,max2;
if(n==1)
*p_max = *p_min = listnumber[0];
else if(n==2)
{
if(listnumber[0] *p_min = listnumber[0];
*p_max = listnumber[1];
}else{
*p_min = listnumber[1];
*p_max = listnumber[0];
}
}else{
minmax(listnumber,n/2,p_min,p_max);
minmax(listnumber+n/2,n-(n/2),&min2,&max2);
if(min2<*p_min)
*p_min = min2;
if(max2>*p_max)
*p_max = max2;
}
}
void main(){
int list[10]={1,5,7,6,3,4,8,9,2,0} ;
int p_max,p_min;
int n=10;
minmax(list,n,&p_min,&p_max);
printf("max = %d : min = %d\n",p_max,p_min);
}
#include
#include
typedef struct dlist{
int element;
struct dlist *next;
struct dlist *prev;
}NODE;
NODE *createNode(int item){
NODE *newNode=NULL;
newNode = (NODE *)malloc(sizeof(NODE));
newNode->element=item;
newNode->next=NULL;
newNode->prev=NULL;
return newNode;
}
NODE *insertNodeHead(NODE *head,int item){
NODE *newNode = NULL;
newNode = createNode(item);
if(head == NULL)
return newNode;
newNode->next = head;
head->prev = newNode;
return newNode;
}
NODE *insertNodeTail(NODE *head,int item){
NODE *newNode = NULL,*runNode = head;
newNode = createNode(item);
if(head == NULL)
return newNode;
while(runNode->next!=NULL)
runNode = runNode->next;
runNode->next = newNode;
newNode->prev = runNode;
return head;
}
NODE *insertNodeBetween(NODE *head,int item,int position){
NODE *newNode =NULL,*runNode = head;
newNode = createNode(item);
if(head ==NULL){
return newNode;
}
else{
while(runNode != NULL && runNode->element!=position)
runNode = runNode->next;
if(runNode == NULL)
printf("position not found [%d]\n",position);
else {
if(runNode->prev ==NULL){ //insert head node
runNode->prev = newNode;
newNode->next = runNode;
head = newNode;
}else{
newNode->next = runNode;
newNode->prev = runNode->prev;
runNode->prev = newNode;
newNode->prev->next = newNode;
}
}
}
return head;
NODE *deleteNode(NODE *head,int item){
NODE *runNode = head;
while(runNode != NULL && runNode->element!=item)
runNode = runNode->next;
if(runNode == NULL){
printf("data not found [%d]\n",item);
}
else {
if(runNode->prev == NULL) { //delete head node
head = head->next; //move next
head->prev = NULL;
}
else {
runNode->prev->next = runNode->next ;
if(runNode->next != NULL)
runNode->next->prev = runNode->prev ;
}
free(runNode);
}
return head;
}
void displayNode(NODE *head){
NODE *runNode = head;
while (runNode != NULL){
printf("[%d]->",runNode->element);
runNode = runNode->next;
}
printf("NULL\n");
}
void main(){
NODE *head=NULL;
printf("insert head node 10 15 90\n");
head = insertNodeHead(head,10);
head = insertNodeHead(head,15);
head = insertNodeHead(head,90);
displayNode(head);
printf("insert tail node 300 400\n");
head = insertNodeTail(head,300);
head = insertNodeTail(head,400);
displayNode(head);
printf("insert between node (955,300) (990,90) (940,400) \n");
head = insertNodeBetween(head,955,300);
head = insertNodeBetween(head,990,90);
head = insertNodeBetween(head,940,400);
head = insertNodeBetween(head,940,500);
displayNode(head);
printf("delete node 90 15 400 500\n");
head = deleteNode(head,90);
head = deleteNode(head,15);
head = deleteNode(head,400);
head = deleteNode(head,500);
displayNode(head);
}
#include
#include
typedef struct slist{
int entry;
struct slist *next;
}NODE;
NODE *createNode(int item)
{
NODE *newNode=NULL;
newNode=(NODE *)malloc(sizeof(NODE));
newNode->next=NULL;
newNode->entry =item;
return newNode;
}
NODE *sort(NODE *head,int item)
{
NODE *newNode=NULL,*runNode=head,*prevNode=NULL;
newNode=createNode(item);
if(head==NULL)
return newNode;
else {
while(runNode->next != NULL && newNode->entry>runNode->entry)
{
prevNode=runNode;
runNode=runNode->next;
}
if(newNode->entry<=runNode->entry){
if(prevNode == NULL){
head=newNode;
head->next=runNode;
}
else{
newNode->next = runNode;
prevNode->next = newNode;
}
}
else{
runNode->next = newNode;
}
}
return head;
}
void displayNodes(NODE *head)
{
NODE *runNode=head;
while(runNode!=NULL)
{
printf("[%d]->",runNode->entry );
runNode=runNode->next;
}
printf("NULL\n");
}
void main(void)
{
int input;
NODE *head=NULL;
// insert head node
// head = sort(head,50);displayNodes(head);
// head = sort(head,40);displayNodes(head);
// head = sort(head,70);displayNodes(head);
// head = sort(head,30);displayNodes(head);
// head = sort(head,60);displayNodes(head);
// head = sort(head,90);displayNodes(head);
do{
printf("input data (if exit input = 999):");
scanf("%d",&input);
if(input!=999)
head=sort(head,input);
}while(input != 999);
displayNodes(head);
}
#include
#include
typedef struct slist{
int entry;
struct slist *next;
}NODE;
NODE *createNode(int item)
{
NODE *newNode=NULL;
newNode=(NODE *)malloc(sizeof(NODE));
newNode->next=NULL;
newNode->entry =item;
return newNode;
}
NODE *insertNodeHead(NODE *head,int item)
{
NODE *newNode=NULL;
newNode=createNode(item);
if(head!=NULL)
newNode->next=head;
return newNode;
}
float calMean(NODE *head)
{
NODE *runNode=head;
int sum=0,count=0;
float ret=0;
while(runNode!=NULL)
{
sum = sum + runNode->entry;
count++;
runNode=runNode->next;
}
ret = (float)sum/count;
return ret;
}
void main(void)
{
int input;
float mean=0;
NODE *head=NULL;
// insert head node
do{
printf("input data (if exit input = 999):");
scanf("%d",&input);
if(input!=999)
head=insertNodeHead(head,input);
}while(input != 999);
if (head!=NULL)
mean=calMean(head);
printf("mean = %f\n",mean);
}