Random speed program in CPP
This program is calculating the random speed explicitly and
implementing the concept of matrix.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
template<typename T>
void print(T values[], int size) {
cout << "[";
for (int i = 0; i < size; i++) {
if (i > 0) cout << ", ";
cout << values[i];
}
cout << "]" << endl;
}
int main() {
// have to initialize the random seed explictly in C++.
srand(time(NULL));
FILE *input = fopen("TestSensingMatrix.txt", "r");
const int NOS = 30;
const int NOC = 2000;
const int MAX_IT = 10;
int SC[NOS][NOC];
memset(SC, 0, sizeof(SC));
// Generate the SC matrix
int row[2];
while (fscanf(input, "%d,%d", &row[0], &row[1]) == 2) {
// printf("%d,%dn", rows[0], rows[1]);
SC[row[0] - 1][row[1] - 1] = 1;
}
// s
double s[NOS];
memset(s, 0, sizeof(s));
for (int x = 0; x < NOS; x++) {
int cnt = 0;
for (int y = 0; y < NOC; y++) {
if (SC[x][y] == 1) {
cnt = cnt + 1;
}
}
s[x] = cnt * 1.0 / NOC;
}
// theta[ai]
double a[NOS];
double b[NOS];
double d = rand() % 12 / 10.0; // randint(0, 11) / 10
double Z[NOC];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(Z, 0, sizeof(Z));
for (int x = 0; x < NOS; x++) {
a[x] = s[x];
b[x] = 0.5 * s[x];
}
print(a, NOS);
print(b, NOS);
for (int itn = 0; itn < MAX_IT; itn++) { // the outer while
// Compute Z(t, j)
for (int j = 0; j < NOC; j++) {
double A = 0;
double B = 0;
// Compute Z
for (int i = 0; i < NOS; i++) {
A = A + SC[i][j] * log(a[i]) + (1-SC[i][j]) * log(1 -
a[i]);
if (b[i] <= 0) {
cout << "I am dumb and don't know how to use log
functions" << endl;
}
B = B + SC[i][j] * log(b[i]) + (1- SC[i][j]) * log(1-
b[i]);
}
A = exp(A);
B = exp(B);
Z[j] = (A * d) / ((A * d) + (B * (1 - d)));
}
// Compute new theta(t + 1)
for (int i = 0; i < NOS; i++) {
double tempa = 0;
double tempb = 0;
double tempd = 0;
double tempz = 0;
double totalz = 0;
int cnt = 0;
for (int j = 0; j < NOC; j++) {
if (SC[i][j] == 1) {
tempz = tempz + Z[j];
cnt = cnt + 1;
}
totalz = totalz + Z[j];
}
a[i] = tempz / totalz;
b[i] = (cnt - tempz) / (NOC - totalz);
if (b[i] < 0) {
cout << i << " is the problem step" << endl;
break;
}
d = totalz / NOC;
}
}
// end of while
cout << d << endl;
print (a, NOS);
print (b, NOS);
FILE *groundtruth = fopen("TestGroundTruth.txt", "r");
int gt[NOC];
memset(gt, 0, sizeof(gt));
while (fscanf(input, "%d,%d", &row[0], &row[1]) == 2) {
// printf("%d,%dn", rows[0], rows[1]);
gt[row[0] - 1] = row[1];
}
int out[NOC];
memset(out, 0, sizeof(out));
FILE *output = fopen("output.txt", "w");
for (int j = 0; j < NOC; j++) {
if (Z[j] >= 0.5) {
out[j] = 1;
}
}
double t[NOS];
memset(t, 0, sizeof(t));
for (int i = 0; i < NOS; i++) {
t[i] = a[i] / (a[i] + b[i]);
cout << t[i] << endl;
}
for (int j = 0; j < NOC; j++) {
fprintf(output, "%d, %dn", j+1, out[j]);
}
fclose(input);
}

C# Assignmet Help

  • 2.
    Random speed programin CPP This program is calculating the random speed explicitly and implementing the concept of matrix. #include <iostream> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> using namespace std; template<typename T> void print(T values[], int size) { cout << "["; for (int i = 0; i < size; i++) { if (i > 0) cout << ", "; cout << values[i]; } cout << "]" << endl; } int main() { // have to initialize the random seed explictly in C++. srand(time(NULL)); FILE *input = fopen("TestSensingMatrix.txt", "r"); const int NOS = 30; const int NOC = 2000; const int MAX_IT = 10; int SC[NOS][NOC]; memset(SC, 0, sizeof(SC)); // Generate the SC matrix int row[2]; while (fscanf(input, "%d,%d", &row[0], &row[1]) == 2) { // printf("%d,%dn", rows[0], rows[1]); SC[row[0] - 1][row[1] - 1] = 1; } // s double s[NOS]; memset(s, 0, sizeof(s)); for (int x = 0; x < NOS; x++) { int cnt = 0; for (int y = 0; y < NOC; y++) { if (SC[x][y] == 1) {
  • 3.
    cnt = cnt+ 1; } } s[x] = cnt * 1.0 / NOC; } // theta[ai] double a[NOS]; double b[NOS]; double d = rand() % 12 / 10.0; // randint(0, 11) / 10 double Z[NOC]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(Z, 0, sizeof(Z)); for (int x = 0; x < NOS; x++) { a[x] = s[x]; b[x] = 0.5 * s[x]; } print(a, NOS); print(b, NOS); for (int itn = 0; itn < MAX_IT; itn++) { // the outer while // Compute Z(t, j) for (int j = 0; j < NOC; j++) { double A = 0; double B = 0; // Compute Z for (int i = 0; i < NOS; i++) { A = A + SC[i][j] * log(a[i]) + (1-SC[i][j]) * log(1 - a[i]); if (b[i] <= 0) { cout << "I am dumb and don't know how to use log functions" << endl; } B = B + SC[i][j] * log(b[i]) + (1- SC[i][j]) * log(1- b[i]); } A = exp(A); B = exp(B); Z[j] = (A * d) / ((A * d) + (B * (1 - d))); } // Compute new theta(t + 1) for (int i = 0; i < NOS; i++) { double tempa = 0; double tempb = 0; double tempd = 0; double tempz = 0;
  • 4.
    double totalz =0; int cnt = 0; for (int j = 0; j < NOC; j++) { if (SC[i][j] == 1) { tempz = tempz + Z[j]; cnt = cnt + 1; } totalz = totalz + Z[j]; } a[i] = tempz / totalz; b[i] = (cnt - tempz) / (NOC - totalz); if (b[i] < 0) { cout << i << " is the problem step" << endl; break; } d = totalz / NOC; } } // end of while cout << d << endl; print (a, NOS); print (b, NOS); FILE *groundtruth = fopen("TestGroundTruth.txt", "r"); int gt[NOC]; memset(gt, 0, sizeof(gt)); while (fscanf(input, "%d,%d", &row[0], &row[1]) == 2) { // printf("%d,%dn", rows[0], rows[1]); gt[row[0] - 1] = row[1]; } int out[NOC]; memset(out, 0, sizeof(out)); FILE *output = fopen("output.txt", "w"); for (int j = 0; j < NOC; j++) { if (Z[j] >= 0.5) { out[j] = 1; } } double t[NOS]; memset(t, 0, sizeof(t)); for (int i = 0; i < NOS; i++) { t[i] = a[i] / (a[i] + b[i]); cout << t[i] << endl; } for (int j = 0; j < NOC; j++) {
  • 5.
    fprintf(output, "%d, %dn",j+1, out[j]); } fclose(input); }