LAB 4 Answers

You might also like

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

// Endeiktiki Lysi tis Askisis 4.1- Ak.

Etos 2020-2021

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

#define MAXNUM 1000

int main(void)
{
int size;
int *matrix;
int i, sum = 0;

/* Arxikopoiisi mixanis tuxaiwn arithmwn */


srand(time(NULL));

printf("Type the size of the matrix: ");


scanf("%d", &size);

/* Desmeuw xwro */
matrix = (int *) malloc(size*sizeof(int));
if (matrix == NULL)
{
printf("Error in malloc\n");
return 1;
}

/* Arxikopoiisi toy pinaka */


for (i=0; i<size; i++)
matrix[i] = rand() % MAXNUM;

/* Ektipwsi toy pinaka kai ipologismos athroismatos twn


* stoixeiwn toy.
*/
for (i=0; i<size; i++)
{
printf("Matrix[%d]=%d\n", i, matrix[i]);
sum += matrix[i];
}

/* Ektipwsi toy mesoy oroy */


printf("Average=%f\n", (float)sum/(float)size);

free(matrix);

return 0;
}
// Endeiktiki Lysi tis Askisis 4.2- Ak. Etos 2020-2021

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

int main()
{
int i, n1 = 0, n2 = 0;
int *a1;

/* Get original array size (n1) */


printf("Enter array size: ");
scanf("%d", &n1);

/* Create dynamically a new array of n1 ints */


a1 = (int *) malloc(n1*sizeof(int));
for (i = 0; i < n1; i++)
{
/* Set each element of a1 equal to 55 */
a1[i] = 55;
}

/* Ask the user to specify the new array size (n2) */


printf("Enter new array size: ");
scanf("%d", &n2);

/* Change the allocated space for array a1 to accomodate the


new n2 size */
a1 = (int *) realloc(a1, n2*sizeof(int));

/* If n2 > n1, set all extra elements to 0 */


for (i = n1; i < n2; i++)
a1[i] = 0;

/* Print */
for (i = 0; i < n2; i++)
printf("%d ", a1[i]);
printf("\n");

/* Free an allocated memory */


free(a1);

return 0;
}
// Endeiktiki Lysi tis Askisis 4.3- Ak. Etos 2020-2021

#include <stdio.h>
#include <string.h> /* strlen(), strcpy() */
#include <stdlib.h> /* For malloc() */

/* size_t is similar to long int */


char *my_strndup(char *s, size_t n)
{
char *new;
int slen;

if (s == NULL) /* Make sure the user gave valid input


*/
return (NULL);

/* We must discover how much space we need and allocate it:


* 1 + the minimum of n and the length of s
*/
slen = strlen(s);
if (slen < n)
new = (char *) malloc(slen + 1);
else
new = (char *) malloc(n + 1);

if (new == NULL) /* Check */


return (NULL);

strncpy(new, s, n); /* Just copy up to n characters */


if (slen >= n) /* We must add the \0 ourselves! (check:
man strncpy) */
new[n] = '\0';

return (new);
}

int main()
{
char arr[100], *copy;

printf("Enter a line of text:\n");


fgets(arr, 100, stdin);
copy = my_strndup(arr, 10);
if (copy != NULL)
printf("Original string: %s\nNew string: %s\n", arr,
copy);
return 0;
}
// Endeiktiki Lysi tis Askisis 4.4- Ak. Etos 2020-2021
/* Pascal triangle * MYY502*/

#include <stdio.h>
#include <stdlib.h> /* For malloc(), free() */

int **pascal(int n); /* Prototype */

int main(int argc, char *argv[])


{
int n, i, j;
int **p;

if (argc < 2)
return 1;
n = atoi(argv[1]);
if (n < 1)
return 1;

p = pascal(n);
if (p == NULL)
return 1;

/* Print */
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
printf("%2d ", p[i][j]);
printf("\n");
}

/* Free memory */
for (i = 0; i < n; i++)
free(p[i]); /* Free row i */
free(p); /* Free the array of pointers */

return 0;
}

int **pascal(int n)
{
int i, j;
int **array;

if (n < 1) return (NULL); /* Cannot do it */

/* Allocate n pointers to point to the rows */


array = (int **) malloc(n * sizeof(int *));
if (array == NULL)
return (NULL);

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


{
/* Allocate a row of size i+1 */
array[i] = (int *) malloc((i+1) * sizeof(int));
if (array[i] == NULL)
{
/* Free memory */
for (j = 0; j < i; j++)
free(array[j]); /* Free row i */
free(array); /* Free the array of
pointers */
return (NULL);
}

/* Calculate */
array[i][0] = 1;
for (j = 1; j < i; j++)
array[i][j] = array[i-1][j-1] + array[i-1][j];
array[i][i] = 1;
}

return (array);
}
// Endeiktiki Lysi tis Askisis 4.5- Ak. Etos 2020-2021

/* Encryption using the ceasar method.


*
* Assume distance d.
* A lowercase ASCII character character c is the i-th
* letter in the English alphabet, where i = c - 'a'.
* The i-th letter is mapped to the (i+d)%26 letter since
* there are 26 letters in the English alphabet. This
* has ASCII code 'a'+(i+d)%26.
*
* To decrypt, we do the reverse procedure and subtract d.
* Thus an encrypted character e is the i-th english letter,
* where i = e-'a'. The original was in distance -d, thus
* equal to (i-d)%26. ***To avoid negative numbers we add 26***,
* i.e. calculate (26+i-d)%26. Add 'a' and you get the ASCII
* code of the original letter.
*/

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

/* If c is a letter, it returns the letter in distance dist */


char mapcharacter(char c, int dist)
{
if (islower(c))
/* (c - 'a') is the position of letter c in the
alphabet (a is 0, b is 1, ...)
* (n + dist) % 26, gives the number dist positions
after n (circularly)
* If
*/
return ( 'a' + (((c-'a') + dist + 26) % 26) );
else
if (isupper(c))
return ( 'A' + (((c-'A') + dist + 26) % 26) );
else
return (c);
}

char *encrypt(char *str, int dist)


{
char *new, *t;

new = (char *) malloc(strlen(str) + 1); /* +1 for the \0 */


if (new == NULL)
exit(1);
for (t = new; *str != '\0'; str++, t++)
*t = mapcharacter(*str, dist);
*t = '\0';
return (new);
}

void decrypt(char *str, int dist)


{
for (; *str != '\0'; str++)
*str = mapcharacter(*str, -dist);
}

int main(int argc, char *argv[])


{
char *s = NULL;
int d = 0;

if (argc != 4)
exit(1); /* Don't do anything */
d = atoi(argv[2]); /* Distance */
if (d < 1 || d > 25)
{
printf("Distance should be > 0 and < 26\n");
exit(1);
}
if (strcmp(argv[1], "encrypt") == 0)
{
s = encrypt(argv[3], d);
printf("%s\n", s);
free(s);
}
else /* Assume it is "decrypt" */
{
decrypt(argv[3], d);
printf("%s\n", argv[3]);
}
return 0;
}

You might also like