дз 1 семестр / ДЗ 7
.docxДомашняя работа №7
Задание 1
Условие задачи:
Для трёх одномерных массивов выполнить сдвиг элементов вправо (влево) на 1 разряд (с подпрограммой).
Текст программы:
#include <iostream>
#include <conio.h>
#include <math.h>
#include <ctime>
using namespace std;
void right(int a[5], int l)
{
for (int i = l - 1; i > 0; i--)
{
a[i] = a[i - 1];
}
a[0] = 0;
}
void main()
{
int a[5] = { 1,2,3,4,5 };
right(a, 5);
for (int i = 0; i < 5; i++)
{
cout << ("%i", a[i]);
}
_getch();
}
Результаты выполнения программы:
Задание 2
Условие задачи:
Пары одномерных массивов, состоящих из нулей и единиц, сложить по правилам двоичной арифметики (с подпрограммой).
Текст программы:
#include <iostream>
#include <ctime>
#include <conio.h>
#include <stdio.h>
#include <math.h>
void sum(int a[6], int b[6], int c[7]) {
int next = 0;
for (int i = 0; i < 6; i++) {
((a[i] + b[i] + next) > 1) ? c[i] = a[i] + b[i] + next - 2 : c[i] = a[i] + b[i] + next;
((a[i] + b[i] + next) > 1) ? next = 1 : next = 0;
}
c[6] = next;
}
int main()
{
srand(time(0));
int A[6], B[6], C[7] = { 0,0,0,0,0,0,0 };
printf(" ");
for (int i = 5; i >= 0; i--) {
A[i] = rand() % (1 - 0 + 1) + 0;
printf(" %3i ", A[i]);
}
printf("\n ");
for (int i = 5; i >= 0; i--) {
B[i] = rand() % (1 - 0 + 1) + 0;
printf(" %3i ", B[i]);
}
sum(A, B, C);
printf("\n");
for (int i = 6; i >= 0; i--) {
printf(" %3i ", C[i]);
}
_getch();
}
Результаты выполнения программы:
Задание 3
Условие задачи:
Разработать программу-модель умножения чисел с фиксированной точкой по правилам двоичной арифметики (с младших разрядов).
Текст программы:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctime>
using namespace std;
void ToArray(int A, int a[6])
{
int i = 0;
while (A > 0)
{
a[i] = (A) % 10;
A = A / 10;
i++;
}
}
int toTen(int a[6])
{
int leng = 6;
int res = 0;
int V = 1;
for (int i = 0; i <= leng - 1; i++)
{
res += a[i] * V;
V = V * 2;
}
return res;
}
void mult(int a[6], int b[6], int c[13])
{
for (int j = 0; j < toTen(b); j++)
{
int next = 0;
for (int i = 0; i < 13; i++)
{
int temp = c[i];
((a[i] + c[i] + next) > 1) ? c[i] = a[i] + c[i] + next - 2 : c[i] = a[i] + c[i] + next;
((a[i] + temp + next) > 1) ? next = 1 : next = 0;
}
c[12] = next;
}
}
void main()
{
int c[13] = { 0,0,0,0,0,0,0,0,0,0,0,0,0 };
int a[12] = { 0,1,0,1,0,0,0,0,0,0,0,0 };
int b[12] = { 0,1,0,0,0,0,0,0,0,0,0,0 };
mult(a, b, c);
for (int i = 12; i >= 0; i--)
{
printf(" %3i ", c[i]);
}
_getch();
}
Результаты выполнения программы:
Задание 4
Условие задачи:
Разработать программу-модель сложения чисел с плавающей точкой по правилам двоичной арифметики.
Текст программы:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <ctime>
using namespace std;
void sm(int a[8], int b[8], int c[8])
{
int next = 0;
for (int i = 0; i < 8; i++)
{
((a[i] + b[i] + next) > 1) ? c[i] = a[i] + b[i] + next - 2 : c[i] = a[i] + b[i] + next;
((a[i] + b[i] + next) > 1) ? next = 1 : next = 0;
}
c[7] = next;
}
void ToRight(int a[8]) {
for (int i = 7; i > 0; i--)
{
a[i] = a[i - 1];
}
a[0] = 0;
}
int sum(int mA[8], int mB[8], int mC[8], int pA, int pB)
{
int pC;
while (pA != pB)
{
(pA > pB) ? ToRight(mB) : ToRight(mA);
(pA > pB) ? pB++ : pA++;
}
sm(mA, mB, mC);
pC = pA;
while (mC[7] != 0)
{
ToRight(mC);
pC++;
}
return pC;
}
void main()
{
_getch();
}
Задание 5
Условие задачи:
Выполнить перевод в дополнительный код (инвертировать биты и добавить единицу в младший разряд) числа представленного массивом.
Текст программы:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctime>
using namespace std;
void dop(int c[7])
{
int one[6] = { 1,0,0,0,0,0 };
for (int i = 0; i < 6; i++)
{
(c[i] == 1) ? c[i] = 0 : c[i] = 1;
}
int next = 0;
for (int i = 0; i < 6; i++)
{
int temp = c[i];
((one[i] + c[i] + next) > 1) ? c[i] = one[i] + c[i] + next - 2 : c[i] = one[i] + c[i] + next;
((one[i] + temp + next) > 1) ? next = 1 : next = 0;
}
c[6] = next;
}
void main()
{
int c[7] = { 0,1,1,0,1,1,0 };
dop(c);
for (int i = 6; i >= 0; i--)
{
printf(" %3i ", c[i]);
}
_getch();
}
Результаты выполнения программы: