Cn Chatgpt Programs
Cn Chatgpt Programs
#include<iostream>
using namespace std;
class Dj {
int n, cost[10][10], d[10], p[10], v[10];
public:
void readMatrix();
void shortestPath(int src);
void display(int src);
};
void Dj::readMatrix() {
cout << "Enter the number of vertices: ";
cin >> n;
cout << "Enter the cost adjacency matrix:\n";
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> cost[i][j];
}
v[u] = 1;
for (int s = 0; s < n; ++s)
if (!v[s] && d[u] + cost[u][s] < d[s])
d[s] = d[u] + cost[u][s], p[s] = u;
}
}
int main() {
Dj dij;
dij.readMatrix();
int source;
cout << "Enter the source: ";
cin >> source;
dij.shortestPath(source);
dij.display(source);
return 0;
}
2.
#include <stdio.h>
#include <string.h>
int main() {
printf("\n\t\t\tAT SOURCE NODE\n\nEnter the data to be sent: ");
gets(data);
strcpy(concatdata, data);
strcat(concatdata, "0000000000000000");
strncpy(divident, concatdata, 17);
divident[17] = '\0';
crc_cal(0);
if (strcmp(dest_crc, res) == 0)
printf("\nReceived frame is error-free.\n");
else
printf("\nReceived frame has one or more errors.\n");
return 0;
}
3.
#include <stdio.h>
struct rtable {
int dist[20], nextnode[20];
} table[20];
int cost[10][10], n;
void distvector() {
int i, j, k, count;
do {
count = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
int new_dist = cost[i][k] + table[k].dist[j];
if (table[i].dist[j] > new_dist) {
table[i].dist[j] = new_dist;
table[i].nextnode[j] = k;
count++;
}
}
}
}
} while (count);
}
int main() {
printf("Enter the number of vertices: ");
scanf("%d", &n);
distvector();
4.
SERVER:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
if (argc < 2) {
fprintf(stderr, "ERROR, no port provided\n");
exit(1);
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
printf("SERVER: Waiting for client...\n");
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");
bzero(buffer, 256);
if (read(newsockfd, buffer, 255) < 0)
error("ERROR reading from socket");
close(newsockfd);
close(sockfd);
return 0;
}
CLIENT:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
if (argc < 3) {
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero(buf, 3000);
if (read(sockfd, buf, 2999) < 0)
error("ERROR reading from socket");
close(sockfd);
return 0;
}
5.
#include <stdio.h>
int h[12];
void genhamcode() {
h[1] = (h[3] + h[5] + h[7] + h[9] + h[11]) % 2;
h[2] = (h[3] + h[6] + h[7] + h[10] + h[11]) % 2;
h[4] = (h[5] + h[6] + h[7]) % 2;
h[8] = (h[9] + h[10] + h[11]) % 2;
printf("\nTransmitted codeword: ");
for (int i = 1; i < 12; i++) printf(" %d", h[i]);
printf("\n");
}
void makeerror() {
int pos;
printf("\nEnter the position to make error: ");
scanf("%d", &pos);
h[pos] ^= 1; // Flip the bit
printf("\nError codeword: ");
for (int i = 1; i < 12; i++) printf(" %d", h[i]);
printf("\n");
}
void correcterror() {
int errpos = (h[1] + h[3] + h[5] + h[7] + h[9] + h[11]) % 2 +
((h[2] + h[3] + h[6] + h[7] + h[10] + h[11]) % 2) * 2 +
((h[4] + h[5] + h[6] + h[7]) % 2) * 4 +
((h[8] + h[9] + h[10] + h[11]) % 2) * 8;
int main() {
printf("\nEnter the message bits: ");
for (int i = 1; i < 12; i++)
if (i == 3 || i == 5 || i == 6 || i == 7 || i == 9 || i == 10 || i == 11)
scanf("%d", &h[i]);
printf("Message: ");
for (int i = 1; i < 12; i++) printf("%d", h[i]);
printf("\n");
genhamcode();
int ch;
printf("\nDo you want to make an error? (0 or 1): ");
scanf("%d", &ch);
if (ch) {
makeerror();
correcterror();
} else {
printf("\nNo error.\n");
}
return 0;
}
6.
SENDER:
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int sock, connected;
char fr[30];
struct sockaddr_in server_addr, client_addr;
socklen_t sin_size;
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(17000);
server_addr.sin_addr.s_addr = INADDR_ANY;
listen(sock, 5);
sin_size = sizeof(client_addr);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
if (connected == -1) {
perror("Accept failed");
close(sock);
exit(1);
}
int i = 1;
while (1) {
printf("Enter Data Frame %d (Enter 'exit' to end): ", i);
scanf("%s", fr);
send(connected, fr, strlen(fr), 0);
if (strcmp(fr, "exit") == 0) break;
recv(sock, fr, sizeof(fr), 0);
printf("Acknowledgment: %s\n", fr);
i++;
}
close(sock);
return 0;
}
RECEIVER:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int sock;
char receive[30];
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname("127.0.0.1");
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("Socket creation failed");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(17000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
memset(&(server_addr.sin_zero), 0, 8);
int i = 1;
while (1) {
int bytes_received = recv(sock, receive, sizeof(receive) - 1, 0);
receive[bytes_received] = '\0';
if (strcmp(receive, "exit") == 0) break;
printf("\nFrame %d data %s received\n", i, receive);
send(sock, (strlen(receive) < 10) ? receive : "negative", (strlen(receive) < 10) ?
strlen(receive) : 8, 0);
i++;
}
close(sock);
return 0;
}
7.
SERVER:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
int main() {
char p[100], c[300], ch;
int fd, fd2, f1, num, i = 0;
mkfifo(FIFO1, 0666);
mkfifo(FIFO2, 0666);
printf("SERVER ONLINE\n");
fd = open(FIFO1, O_RDONLY);
printf("Client online\nWaiting for request...\n");
close(fd);
unlink(FIFO1);
unlink(FIFO2);
return 0;
}
RECEIVER:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
int main() {
char p[100], c[3000];
int fd, fd2, num;
mkfifo(FIFO1, 0666);
mkfifo(FIFO2, 0666);
printf("Waiting for server...\n");
fd = open(FIFO1, O_WRONLY);
printf("SERVER ONLINE!\nCLIENT: Enter the path\n");
close(fd);
unlink(FIFO1);
unlink(FIFO2);
return 0;
}
8.
SERVER:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (argc < 2) {
fprintf(stderr, "ERROR, no port provided\n");
exit(0);
}
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));
return 0;
}
CLIENT:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (argc != 3) {
printf("Usage: server port\n");
exit(1);
}
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0) error("Unknown host");
11.
#include <stdio.h>
#include <stdlib.h>
int main() {
int cap, oprt, cont = 0, inp[MAX], ch, nsec = 0, drop = 0;
while (1) {
printf("Enter the number of packets entering at %d seconds: ", nsec + 1);
scanf("%d", &inp[nsec++]);
printf("Enter 1 to insert packet or 0 to quit: ");
scanf("%d", &ch);
if (!ch) break;
}
12.
SERVER:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main() {
struct sockaddr_in server, client;
int s, n;
char b1[100], b2[100];
struct hostent *hen;
char *IPaddr;
server.sin_family = AF_INET;
server.sin_port = htons(5000);
server.sin_addr.s_addr = INADDR_ANY;
n = sizeof(client);
printf("DNS is ready...\n");
while (1) {
recvfrom(s, b1, sizeof(b1), 0, (struct sockaddr *)&client, &n);
hen = gethostbyname(b1);
if (hen) {
IPaddr = inet_ntoa(*((struct in_addr *)hen->h_addr));
strcpy(b2, IPaddr);
sendto(s, b2, sizeof(b2), 0, (struct sockaddr *)&client, n);
} else {
strcpy(b2, "Not found");
sendto(s, b2, sizeof(b2), 0, (struct sockaddr *)&client, n);
}
}
return 0;
}
CLIENT:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main() {
struct sockaddr_in server;
int s, n;
char b1[100], b2[100];
server.sin_family = AF_INET;
server.sin_port = htons(5000);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
n = sizeof(server);
while (1) {
printf("Enter domain name: ");
scanf("%s", b2);
sendto(s, b2, strlen(b2) + 1, 0, (struct sockaddr *)&server, n);
recvfrom(s, b1, sizeof(b1), 0, NULL, NULL);
printf("DNS IP address: %s\n", b1);
}
return 0;
}