You are on page 1of 5

++C ‫ קורס‬1 ‫מטלת בית‬

208133116 ‫ז‬.‫דור אגבבה ת‬


209959220 ‫דוד סומחייב‬
1 ‫שאלה‬
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int Size ;

double WeightedAverage(double* value, double* weight)


{
if (value == NULL || weight == NULL)
return 0;
if ((int)sizeof(value) != (int)sizeof(weight))
return 0;
double mone=0, mehane=0, sum=0;
int i = 0,counter=4;
for (int i = 0; i < counter; i++)//makes average by the formula
{
mone = (value[i] * weight[i]) + mone;
mehane = weight[i] + mehane;
}
sum = mone / mehane;
return sum;
}
void main()
{
Size = 4;
double arr1[4], arr2[4];
arr1[0] = 0.2;
arr1[1] = 0.3;
arr1[2] = 0.4;
arr1[3] = 0.5;
arr2[0] = 2;
arr2[1] = 3;
arr2[2] = 4;
arr2[3] = 5;
cout << "average "<< WeightedAverage(arr2, arr1);

}
2 ‫שאלה‬
#include <iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS\

bool IsArithmeticProgression(int* nums, int numof=3)//gets array of number and check


if it's mathmatic series
{
if (nums == NULL)
return 0;
int dis = nums[1]-nums[0];
for (int i = 1; i < numof - 1; i++)//run until one before end, to not overflow
{
if ((nums[i + 1] - nums[i])!= dis)//checks that the next one has the
same distance as all
return 0;
}
return 1;//return true if all run as well
}

bool IsArithmeticProgression(char* chars, int numof = 3)//gets array of number and


check if it's mathmatic series
{
if (chars == NULL)
return 0;
int* copy= new int[numof];
for (int i = 0; i < numof; i++)// make casting from chars to int in s copy
array
{
copy[i] = (int)chars[i];
}
bool result = IsArithmeticProgression(copy, numof);//use the prev function to
the copy
delete[] copy;
return result;
}
void main()
{
bool ans;
int size = 0, type=0;
int tmpNum = 0;
cout << "insert size of array , at least 3: ";
cin >> size;
if (size < 3)
size = 3;
cout << "insert type: 1(numbers) or 2(letters)" << endl;
cin >> type;
if (type == 1)
{
int* arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "insert number " << endl;
cin >> arr[i];
}
if (size == 3)
ans = IsArithmeticProgression(arr);
else
ans = IsArithmeticProgression(arr, size);
}
else
{
char* arr = new char[size];
for (int i = 0; i < size; i++)
{
cout << "insert 1 char " << endl;
cin >> arr[i];
}
if (size == 3)
ans = IsArithmeticProgression(arr);
else
ans = IsArithmeticProgression(arr, size);
}
if (ans)
cout << "the Array is Arithmetic series";
else
cout << "the Array is not Arithmetic series";
}

3 ‫שאלה‬
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <cstring>

void CopyStrToStr(char* source, char* destination, int size)//external function that


copy by size from on "source"to "destination
{
for (int i = 0; i < size; i++)
{
destination[i] = source[i];
}
destination[size] = '\0';
}

char** wordcutter(char* str, int* size)//takes sentence and make array of words, saves
the size of the array into *size
{
if (str == NULL)
return NULL;
int numofletters = 1;
int j = 0;
while (str[j] != '\0')//count amount of letters
{
j++;
numofletters++;
}

*size = 1;
for (int i = 0; i <numofletters; i++)//count amount of words
{
if (str[i] == ' ')
(*size)++;
}
char** words = new char*[*size];
int startOfWord = 0;
int wordindex = 0;
for (int i = 0; i < numofletters; i++)
{
if (str[i] == ' '|| str[i] == '\0')
{
words[wordindex] = new char[i - startOfWord+1];

CopyStrToStr((str + startOfWord), words[wordindex], i -


startOfWord);
startOfWord = i + 1;
wordindex++;
}
}
return words;
}
void makeitdouble(char* &word)//makes new array in double size and add it to the
original string
{
if (word == NULL)
return;
int numofletters = 1;
int j = 0;
while (word[j] != '\0')
{
j++;
numofletters++;
}
int sizeword = (numofletters*2)-1;
char* tmp = new char[sizeword];
strcpy(tmp, word);
strcat(tmp, word);
delete(word);
word = tmp;

void removememory(char** a, int size)


{
int i = 0;
while (i != size - 1)
{
delete[] a[i];
i++;
}
}
void main()
{
char name[] = "dor is not a good friend";
int number;
char** newone = wordcutter(name, &number);
for (int i = 0; i < number; i++)
{
cout << newone[i]<<endl;
makeitdouble(newone[i]);
cout << newone[i] << endl;
}
removememory(newone,number);
delete[] newone;

You might also like