First Upload
This commit is contained in:
1677
12/PointersLibs.h
Normal file
1677
12/PointersLibs.h
Normal file
File diff suppressed because it is too large
Load Diff
44
12/ex1.c
Normal file
44
12/ex1.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include "PointersLibs.h"
|
||||
|
||||
int * EvenSubArray(int vec[], int len, int *ptr_even)
|
||||
{
|
||||
int * ptr_even_vec = (int)malloc(sizeof(int));
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < len; counter++)
|
||||
{
|
||||
if (!(vec[counter] % TWO))
|
||||
{
|
||||
(*ptr_even)++;
|
||||
ptr_even_vec = realloc(ptr_even_vec, (*ptr_even));
|
||||
*(ptr_even_vec++) = vec[counter];
|
||||
}
|
||||
}
|
||||
|
||||
return (ptr_even_vec);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// exe 1
|
||||
// -----
|
||||
//
|
||||
// General : The program gets an array and separates the even
|
||||
// numbers from it to the pointer.
|
||||
//
|
||||
// Input : an array of numbers, its length.
|
||||
//
|
||||
// Process : the program goes over the array and puts the even nums
|
||||
// in the pointer array.
|
||||
//
|
||||
// Output : if there are even nums, and if there are, print them.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Proggramer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
|
||||
}
|
||||
58
12/ex2.c
Normal file
58
12/ex2.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include "PointersLibs.h"
|
||||
|
||||
void SearchLongestWord(char s[], char **l)
|
||||
{
|
||||
*l = malloc(sizeof(char));
|
||||
char *ptr_s = s,
|
||||
*ptr_temp_s,
|
||||
*ptr_last_char = LastCharOfString(s);
|
||||
unsigned short max_lenght = ZERO;
|
||||
while (ptr_s < ptr_last_char)
|
||||
{
|
||||
|
||||
ptr_temp_s = IndexCharInPointers(ptr_s, ptr_last_char, ' ');
|
||||
if (ptr_temp_s - ptr_s > max_lenght)
|
||||
{
|
||||
max_lenght = ptr_temp_s - ptr_s;
|
||||
*l = realloc(*l, max_lenght);
|
||||
CopyPointers(*l, ptr_s, ptr_temp_s);
|
||||
(*LastCharOfString(*l)) = BACKSLASH_ZERO;
|
||||
}
|
||||
ptr_s = ptr_temp_s + ONE;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// exe 2
|
||||
// -----
|
||||
//
|
||||
// General : the program gets a string and searches for the longest
|
||||
// word in it and saves it.
|
||||
//
|
||||
// Input : a string and a pointer of pointers.
|
||||
//
|
||||
// Process : the program goes over the array and checks if each
|
||||
// word finds is longer than the last one that
|
||||
// was longest.
|
||||
//
|
||||
// Output : the longest word.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Proggramer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
typedef char string[MAX_SIZE_STRING];
|
||||
|
||||
string s = "Your feedback plays an important role in helping other buyers choose products and sellers.";
|
||||
char f;
|
||||
char *f1 = &f;
|
||||
char *f2 = &f1;
|
||||
SearchLongestWord(s, f2);
|
||||
printf("%s\n", f1);
|
||||
free(f1);
|
||||
}
|
||||
47
12/ex3.c
Normal file
47
12/ex3.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include "PointersLibs.h"
|
||||
|
||||
void InsertNumberToRowInMat(int *ptr_mat, unsigned short mat_col, unsigned short custom_row, int number)
|
||||
{
|
||||
ptr_mat = ptr_mat + (custom_row * (mat_col));
|
||||
while (mat_col)
|
||||
{
|
||||
*(ptr_mat + mat_col--) = number % TEN;
|
||||
number /= TEN;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// exe 3
|
||||
// -----
|
||||
//
|
||||
// General : The program takes the last digits according to input
|
||||
// from each number inputted.
|
||||
//
|
||||
// Input : how many numbers to take and input, and how many digits
|
||||
// at least for the input.
|
||||
//
|
||||
// Process : the program gets input from user, then takes the
|
||||
// last numbers according to input, and makes a char
|
||||
// matrix out of it.
|
||||
//
|
||||
// Output : numbers in a matrix looking form.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Proggramer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
unsigned short HowManyNums = 4,
|
||||
HowManyDigits = 3;
|
||||
int *mat = malloc(sizeof(int) * HowManyNums * HowManyDigits);
|
||||
|
||||
InsertNumberToRowInMat(mat, HowManyDigits, 0, 167367);
|
||||
InsertNumberToRowInMat(mat, HowManyDigits, 1, 6376);
|
||||
InsertNumberToRowInMat(mat, HowManyDigits, 2, 2462172);
|
||||
InsertNumberToRowInMat(mat, HowManyDigits, 3, 64036);
|
||||
free(mat);
|
||||
}
|
||||
48
12/ex4.c
Normal file
48
12/ex4.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include "PointersLibs.h"
|
||||
|
||||
void EvenAndOddToTwoVec(int vec[], unsigned short vec_size, int *evensize, int *oddsize, int **even, int **odd)
|
||||
{
|
||||
*even = malloc(sizeof(int));
|
||||
*odd = malloc(sizeof(int));
|
||||
*evensize = ZERO;
|
||||
*oddsize = ZERO;
|
||||
unsigned short counter;
|
||||
for (counter = ZERO; counter < vec_size; counter++)
|
||||
{
|
||||
if (!(vec[counter] % TWO))
|
||||
{
|
||||
*even = realloc(*even, ++(*evensize));
|
||||
*((*even) + (*evensize) - ONE) = vec[counter];
|
||||
}
|
||||
else
|
||||
{
|
||||
*odd = realloc(*odd, ++(*oddsize));
|
||||
*((*odd) + (*oddsize) - ONE) = vec[counter];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// exe 4
|
||||
// -----
|
||||
// General : the program gets an array of numbers, and breaks it
|
||||
// down to even numbers and odd numbers.
|
||||
//
|
||||
// Input : an array of numbers.
|
||||
//
|
||||
// Process : goes over the arrau and adds the even num to the even
|
||||
// pointer and the odd to odd pointer.
|
||||
//
|
||||
// Output : The even array and odd array.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Proggramer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
|
||||
}
|
||||
48
12/ex5.c
Normal file
48
12/ex5.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include "PointersLibs.h"
|
||||
|
||||
|
||||
BOOLEAN SentenceALowerThenSentenceB(char *ptr_sentence_a, char *ptr_sentence_b)
|
||||
{
|
||||
while (*(ptr_sentence_a++) == *(ptr_sentence_b++) && *ptr_sentence_a && *ptr_sentence_b);
|
||||
|
||||
return ((*(--ptr_sentence_a) <= *(--ptr_sentence_b)));
|
||||
}
|
||||
|
||||
void SwapPointers(char **first_pointer, char **second_pointer)
|
||||
{
|
||||
char *temp_pointer = *first_pointer;
|
||||
*first_pointer = *second_pointer;
|
||||
*second_pointer = temp_pointer;
|
||||
}
|
||||
|
||||
void SortStringAsDictionary(char **ptr_vec, unsigned short lenght)
|
||||
{
|
||||
char **low_sentence = ptr_vec;
|
||||
unsigned short counter_loop1, counter_loop2;
|
||||
for (counter_loop1 = ZERO; counter_loop1 < lenght; counter_loop1++)
|
||||
{
|
||||
for (counter_loop2 = counter_loop1; counter_loop2 < lenght; counter_loop2++)
|
||||
{
|
||||
(SentenceALowerThenSentenceB(*ptr_vec ,*low_sentence) ? ZERO : SwapPointers(ptr_vec, low_sentence));
|
||||
*low_sentence = LastCharOfString(*low_sentence);
|
||||
}
|
||||
*ptr_vec = LastCharOfString(*ptr_vec) + ONE;
|
||||
*low_sentence = *ptr_vec;
|
||||
}
|
||||
}
|
||||
// --------------------------------------------------------------------------------------
|
||||
// Exe 5
|
||||
// -----
|
||||
//
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Proggramer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 21.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
|
||||
}
|
||||
116
12/ex6.c
Normal file
116
12/ex6.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "PointersLibs.h"
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
|
||||
void Swap(int *num1, int *num2)
|
||||
{
|
||||
//swaps between two numbers
|
||||
int temp = *num1;
|
||||
*num1 = *num2;
|
||||
*num2 = temp;
|
||||
}
|
||||
|
||||
void SortMat(int *mat,int max_size)
|
||||
{
|
||||
//sorts them by ascending order
|
||||
for (int count = ZERO; count < max_size; count++)
|
||||
{
|
||||
for (int counter = count + ONE; counter < max_size; counter++)
|
||||
{
|
||||
(*(mat + count) > *(mat + counter)) ?
|
||||
Swap((mat + count) ,(mat + counter)): ZERO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Switcharoo(int *begin,int *stop)
|
||||
{
|
||||
//switches between numbers in a certain
|
||||
while (stop > begin)
|
||||
{
|
||||
int temp = *begin;
|
||||
*begin = *stop;
|
||||
*stop = temp;
|
||||
stop--;
|
||||
begin++;
|
||||
}
|
||||
}
|
||||
|
||||
void Special_Print_for_sixth(int *mat,int size)
|
||||
{
|
||||
//special print for ex 6
|
||||
int amnt = size*size;
|
||||
for (int count = ZERO; count < size; count++)
|
||||
{
|
||||
for (int counter = count; counter < size*size; counter += size)
|
||||
{
|
||||
printf("%d ", *(mat + counter));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void PutIn(int *mat,int num,int N)
|
||||
{
|
||||
*mat = num;
|
||||
int maxsize = N * N;
|
||||
SortMat(mat,maxsize);
|
||||
for (int count = ZERO; count < maxsize; count++)
|
||||
{
|
||||
//switches the even rows values places
|
||||
//so that when you print it shows ok
|
||||
((count / N) % TWO == ONE) ? Switcharoo((mat + count), (mat + count + N - ONE)) : ZERO;
|
||||
((count / N) % TWO == ONE) ? count += N - ONE : ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// exe 6
|
||||
// -----
|
||||
//
|
||||
// General : the program sorts the numbers in an ascending order
|
||||
// and then adds a number that is desiered to the matrix, sorts it
|
||||
// again.
|
||||
//
|
||||
// Input : size o the matrix, and N^2 of numbers.
|
||||
//
|
||||
// Process : sorts in ascending order and then sorts them according to the wanted way.
|
||||
//
|
||||
// Output : prints the new matrix.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Proggramer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 22.11.19
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
int N = ZERO;
|
||||
int *mat = (int*)malloc(sizeof(int));
|
||||
printf("enter Size: ");
|
||||
scanf("%d", &N);
|
||||
int maxsize = N * N;
|
||||
int num = ZERO;
|
||||
for (int sofer = ZERO; sofer < maxsize; sofer++)
|
||||
{
|
||||
mat = realloc(mat, sizeof(int)*(sofer+ONE));
|
||||
printf("enter no.%d: ",sofer+ONE);
|
||||
scanf("%d", &(*(mat+sofer)));
|
||||
}
|
||||
SortMat(mat,maxsize);
|
||||
for (int count = ZERO; count < maxsize; count++)
|
||||
{
|
||||
//switches the even rows values places
|
||||
//so that when you print it shows ok
|
||||
((count / N) % TWO == ONE) ? Switcharoo((mat + count), (mat + count + N - ONE)) : ZERO;
|
||||
((count / N) % TWO == ONE) ? count += N - ONE : ZERO;
|
||||
}
|
||||
Special_Print_for_sixth(mat, N);
|
||||
printf("enter number to enter: ");
|
||||
scanf("%d", &num);
|
||||
PutIn(mat, num, N);
|
||||
Special_Print_for_sixth(mat, N);
|
||||
free(mat);
|
||||
}
|
||||
Reference in New Issue
Block a user