C Programming
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// These defines do a text replacement
// everytime the string 'ROWS' and 'COLUMNS'
// are found in this specific source file.
// You can play with these values.
#define ROWS 5
#define COLUMNS 5
/* ============= Tutorial on Graph format ============
You are given a randomly generated graph that looks
of the form:
0 0 1 1 1
1 0 0 1 1
0 1 0 1 1
1 0 0 0 0
1 0 1 1 0
You can think of the graph with labels for each of
the nodes (sometimes called vertices) more clearly below:
0 1 2 3 4
---------
node 0: 0 0 1 1 1
node 1: 1 0 0 1 1
node 2: 0 1 0 1 1
node 3: 1 0 0 0 0
node 4: 1 0 1 1 0
A '1' represents a connection to a node, and a 0
means it is not connected.
0 1 2 3 4
---------
node 0: 0 0 1 1 1
node 1: 1 0 0 1 1
node 2: 0 1 0 1 1
node 3: 1 0 0 0 0
node 4: 1 0 1 1 0
In the above, '0' is connected to 2, 3, and 4.
'1' is connected to '0', '3', and '4'.
etc.
The number of connections a node has is its 'degree'
*/
// This function generates a random graph
// Note: For the purpose of this lab,
// we do not seed the random number generator
// so we can generate the same graph over
// and over again.
void generateGraph(int* g){
// Uncomment the line below if you want
// to generate a random graph each time.
// srand(time(NULL));
int i,j;
for(i=0; i < ROWS; i++){
for(j=0; j < COLUMNS; j++){
if(i==j){
g[i*COLUMNS+j] = 0; // Why this condition?
}else{
g[i*COLUMNS+j] = rand() % 2;
}
}
}
}
// This function will print out the adjaceny
// matrix for a graph.
void printGraph(int* g){
int i,j;
for(i=0; i < ROWS; i++){
for(j=0; j < COLUMNS; j++){
printf("%d ",g[i*COLUMNS+j]);
}
printf("\n");
}
}
// Compute 'degree' of a node
// For a given node 'n' in an adjaceny matrix,
// compute the degree.
int nodeDegree(int* g, int node){
// TODO
return 999999;
}
// Figure out if two nodes are connected
// Returns a 1 if node1 is connected to node 2
int isConnected(int* g, int node1, int node2){
// TODO
return 0;
}
int main(){
int g_testgraph[ROWS][COLUMNS];
// Generate a random graph
generateGraph(&g_testgraph);
// Print out the graph
printGraph(&g_testgraph);
// Print out the degree of each of the
// five nodes
int i;
for(i =0; i < COLUMNS; ++i){
printf("node %d degree=
%d\n",i,nodeDegree(&g_testgraph,i));
}
// Check which nodes '0' is connected to
printf("Node 0 connections:");
int j;
for(j =0; j < COLUMNS; ++j){
printf("%d ",isConnected(&g_testgraph,0,j));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// These defines do a text replacement
// everytime the string 'ROWS' and 'COLUMNS'
// are found in this specific source file.
// You can play with these values.
#define ROWS 5
#define COLUMNS 5
/* ============= Tutorial on Graph format ============
You are given a randomly generated graph that looks
of the form:
0 0 1 1 1
1 0 0 1 1
0 1 0 1 1
1 0 0 0 0
1 0 1 1 0
You can think of the graph with labels for each of
the nodes (sometimes called vertices) more clearly below:
0 1 2 3 4
---------
node 0: 0 0 1 1 1
node 1: 1 0 0 1 1
node 2: 0 1 0 1 1
node 3: 1 0 0 0 0
node 4: 1 0 1 1 0
A '1' represents a connection to a node, and a 0
means it is not connected.
0 1 2 3 4
---------
node 0: 0 0 1 1 1
node 1: 1 0 0 1 1
node 2: 0 1 0 1 1
node 3: 1 0 0 0 0
node 4: 1 0 1 1 0
In the above, '0' is connected to 2, 3, and 4.
'1' is connected to '0', '3', and '4'.
etc.
The number of connections a node has is its 'degree'
*/
// This function generates a random graph
// Note: For the purpose of this lab,
// we do not seed the random number generator
// so we can generate the same graph over
// and over again.
void generateGraph(int* g){
// Uncomment the line below if you want
// to generate a random graph each time.
// srand(time(NULL));
int i,j;
for(i=0; i < ROWS; i++){
for(j=0; j < COLUMNS; j++){
if(i==j){
g[i*COLUMNS+j] = 0; // Why this condition? Answer: a node is not
connected to itelf, avoid self loop
}else{
g[i*COLUMNS+j] = rand() % 2;
}
}
}
}
// This function will print out the adjaceny
// matrix for a graph.
void printGraph(int* g){
int i,j;
for(i=0; i < ROWS; i++){
for(j=0; j < COLUMNS; j++){
printf("%d ",g[i*COLUMNS+j]);
}
printf("\n");
}
}
// Compute 'degree' of a node
// For a given node 'n' in an adjaceny matrix,
// compute the degree.
int nodeDegree(int* g, int node){
int i = node, j ;
int deg = 0;
for(j = 0; j < COLUMNS; j++){
if(g[i*COLUMNS+j] != 0)
deg++;
}
return deg;
}
// Figure out if two nodes are connected
// Returns a 1 if node1 is connected to node 2
int isConnected(int* g, int node1, int node2){
int i = node1, j = node2;
return g[i*COLUMNS + j];
}
int main(){
int g_testgraph[ROWS][COLUMNS];
// Generate a random graph
generateGraph(&g_testgraph[0][0]);
// Print out the graph
printGraph(&g_testgraph[0][0]);
// Print out the degree of each of the
// five nodes
int i;
for(i =0; i < COLUMNS; ++i){
printf("node %d degree=
%d\n",i,nodeDegree(&g_testgraph[0][0],i));
}
// Check which nodes '0' is connected to
printf("Node 0 connections:");
int j;
for(j =0; j < COLUMNS; ++j){
printf("%d ",isConnected(&g_testgraph[0][0],0,j));
}
return 0;
}
output
----
0 1 1 0 0
1 0 0 0 0
0 0 0 1 1
1 1 1 0 1
1 0 1 0 0
node 0 degree= 2
node 1 degree= 1
node 2 degree= 2
node 3 degree= 4
node 4 degree= 2
Node 0 connections:0 1 1 0 0
C Programming #include <stdio.h> #include <stdlib.h> #include <time.h> // These defines do a text repl...
IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...
#include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...
Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...
Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row major order. Tries to ensure even spacing. void print_grid(int * my_array, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if(my_array[i*cols + j]!=-1){ printf(" %d ", my_array[i*cols + j]); } else{ printf("%d ", my_array[i*cols + j]); } } printf("\n"); } } int main(int argc, char *argv[]) { int seed,rows,cols;...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...
Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main() { int i, j, n; int A[] = {2, 3, 5, 5, 5, 6, 8, 11, 11, 11}; n = sizeof(A) / sizeof(int); for (i = 0; i < n - 1; i++){ if (A[i] != A[i + 1]) { for (j = 1; j < n - 1; j++) { A[j]...
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...
what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void func(int **a) {int tmp; for (int i = 0; i < size; i++) {for (int j = i; j < size, j++) {tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp;}}} int main() {int **arr = malloc(sizeof(int*) * size); for(int i = 0; i < size; i++) {arr[i] = malloc(sizeof(int) * size); for (int j = 0; j < size, j++) arr[i][j] = 2*i...
Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...