3h GRAPTI ERGASIA 2015-16 ANS

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

...

2015-2016

05/03/2016
- 3

1
.
printf("Sum of the first and last digit is %d", n%10 + n/10000);

.
printf("Range is %d - %d", (n/10)*10, (n/10)*10 + 9);

.
f1(8) = f1(7)+8 = (f1(6)+8)+8 = ((f1(2)+6)+8)+8 = (((f1(1)+8)+6)+8)+8 = (((1+8)+6)+8)+8 = 31

v f1(v) v%3
8 f1(8) 8%3=2 f1(8-1)+8= f(7)+8
7 f1(7) 7%3=1 f1(7-1)+8= f(6)+8
6 f1(6) 6%3=0 f1(6/3)+6= f(2)+6
2 f1(2) 2%3=3 f1(2-1)+8= f(1)+8
1 f1(1) 1

.
#include <stdio.h>

int main()
{
int i;
long int term=9,sum=0;
for(i=1;i<=9;i++)
{
sum+=term; /* */
term=(term*10) + 9; /* */
}
printf("%ld",sum);
return (0);
}

.
#define N 20
#include <stdio.h>
struct person
{
char name[16];
char sname[16];
int numberOfChild;
};

int main()
{
struct person p[N];
int i;
printf("Enter info. of %d people\n", N);
for(i=0;i<N;i++)
{
printf("\nEnter name of person %d: ",i+1);
scanf("%15s", p[i].name);

2 8
- 3

printf("Enter surname of person %d: ",i+1);


scanf("%15s", p[i].sname);
do
/*
*/
{
printf("Enter the number of children for person %d: ",i+1);
scanf("%d",&p[i].numberOfChild);
} while (p[i].numberOfChild<0);
}
printf("\n People with more than three children are:");
for(i=0;i<N;i++)
{
if(p[i].numberOfChild>=4)
{
printf("\n Name =%s",p[i].name);
printf("\n Surname =%s",p[i].sname);
}
}
return (0);
}

2
.
#define N 20
#include <stdio.h>

int main()
{
int a[N],b[N],freq[N],i, j, k, found, t;
/* a */
for(i=0;i<N;i++)
{
do /*
*/
{
printf("enter element %d: ",i+1);
scanf("%d", &a[i]);
} while (a[i]<=0||a[i]>=10);
}
/* k
a */
k=0;
/* b
a */
for(i=0;i<N;i++)
{
found = 0;
for(j=0;j<k;j++)
{
if(a[i] == b[j]) /* */
3 8
- 3

{
freq[j]++;
/* b[j] 1 */
found = 1;
break;
}
}
if(found == 0) /* */
{
b[k] = a[i];
freq[k++] = 1; /* 1 */
}
}
for(i=0;i<k-1;i++)
for(j=i+1;j<k;j++) /* */
if(freq[i] < freq[j])
{
t = b[i];
b[i] = b[j];
b[j] = t;
t = freq[i];
freq[i] = freq[j];
freq[j] = t;
}
for(i=0;i<k;i++)
printf("%d\t%d\n",b[i],freq[i]);
return(0);
}

.
#include <stdlib.h>
#include <stdio.h>

struct list {
int value;
struct list * next;
};
typedef struct list item;

int main()
{
item * cur, * head;
int i, sum=0;
head = NULL;
for(i=0;i<10;i++)
/* */
{
cur = (item *)malloc(sizeof(item));
cur->value = 10-i;
cur->next = head;
head = cur;
}
cur = head;
4 8
- 3

while(cur)
{
sum =sum+ cur->value;
cur= cur->next;
}
printf("%d\n", sum);
return(0);
}

.
#include <stdio.h>
#define N 9
int main()
{
int i,j,k,l;
for(i=N;i>=1;i--)
{
for(j=1;j<=i;j++) /* */
printf("%d",j);
for(k=1;k<=2*(N-i)-1;k++) /* */
printf(" ");
for(l=i;l>=1;l--) /* */
{
if(l==N)
continue;
printf("%d",l);
}
printf("\n"); /* */
}
return (0);
}

3
#include <stdio.h>
#include <stdlib.h> /* pause */
#define N 10 /* () */

/* demand */
int DEMAND[N]= {1400,1100,1500,1500,2000,1800,1600,1300,1900,1900};
/* */
void exponential_smoothing(float f[], int size, float a);
/* */
void display_error_analysis(float f[], int size);

/* */
int main()
{
float alfa;
float forecasts[N+1];
5 8
- 3

do
{
/*
'' */
printf("Give smoothing factor a (0<a<1):");
scanf("%f", &alfa);
} while (alfa<=0 || alfa>=1);
exponential_smoothing(forecasts, N, alfa);
display_error_analysis(forecasts, N);
system("pause");
return (0);
}

/* */
void exponential_smoothing(float f[], int size, float a)
{
int t; /* */
f[0]=DEMAND[0];
/* H 1
. */
for (t=1; t<=size; t++)
f[t]=f[t-1]+a*(DEMAND[t-1]-f[t-1]);
/* t.
f 2015 */
}

/* */
void display_error_analysis(float f[], int size)
{
int t; /* */
int year = 2005; /* year 2005
1 */
printf("\n.....FORECASTING ANALYSIS.....\n");
printf(" t Year Demand Forecast Error Absolute Squared\n");
printf("-------------------------------------------------\n");
float sum1=0;
float sum2=0; /* */
for (t=0; t<size; t++)
{
printf("%2d %d %d %-8.2f",t+1,year+t,DEMAND[t],f[t]);
printf(" %-8.2f %-8.2f%-8.2f\n", DEMAND[t]-f[t],
(DEMAND[t]-f[t]>0?DEMAND[t]-f[t]:f[t]-DEMAND[t]),
(DEMAND[t]-f[t])*(DEMAND[t]-f[t]));
sum1+= (DEMAND[t]-f[t]>0?DEMAND[t]-f[t]:f[t]-DEMAND[t]);
/* */
sum2+=(DEMAND[t]-f[t])*(DEMAND[t]-f[t]);
/* */
}
printf("-------------------------------------------\n");
printf(" MAE= %-8.2f\n", sum1/(float)size);
printf(" MSE= %-8.2f\n", sum2/(float)size);
printf("\n 2015 forecast=%-8.2f\n\n", f[size]);
}
6 8
- 3

4
#include <stdio.h>
#include <stdlib.h>

int validnumber(int, char*);


unsigned long base2dec(int, char*);
void dec2base(int, unsigned long);

int main()
{
int base; /* */
unsigned long decnumber; /* */
char s[9]; /* */
int i; /* */

do /* base */
{
printf("insert base: ");
scanf("%d", &base);
}
while ((base < 2) || (base > 16));

do /* */
{
printf("insert number: ");
scanf("%8s",s);
}
while (!validnumber(base, s));

decnumber = base2dec(base, s); /* */


printf("\n"); /* 2 16 */
for(i=2; i<=16; i++)
dec2base(i, decnumber);
system("pause");
return(0);
}

int validnumber(int b, char* p)/* */


{
int valid = 1; /* 1 */
do /* */
{
if (b <= 10)
{
if (!((*p >= '0') && (*p <'0'+b)))
valid = 0;
}
else
{
if (!((*p>='0')&&(*p<='9'))&& !((*p>='A')&&(*p<'A'+b-10)))
valid = 0;
}
7 8
- 3

p++;
}
while ((*p != '\0') && (valid == 1));
return(valid);
}

unsigned long base2dec(int b, char *p) /* 10 */


{
int x; /* */
if (*p <= '9')
x = *p - '0';
else
x = *p-'A'+10;
while (*(++p) != '\0') /* */
{ /* */
if (*p <= '9')
*p = *p - '0';
else
*p = *p -'A'+10;
x = b * x + *p;
}
return(x);
}

void dec2base(int b, unsigned long x)


/* 10 b */
{
int ypol[32]; /* b */
int i = 0; /* */
int n; /* */
do /* b */
{
ypol[i++] = x % b;
x /= b;
}
while (x != 0);

printf("base: %2d number = ", b); /* b */


for(n = i-1; n >= 0; n--) /* */
if (ypol[n] < 10) /* */
printf("%1d", ypol[n]);
else
printf("%c", 'A'-10+ypol[n]);
printf("\n");
}

8 8

You might also like