Havel-Hakimi Algorithm in C - c

I'm trying to write havel-hakimi theorem in C. But I have a problem with the while loop. The program doesn't sort array again in the while loop and that's why output prints the wrong answer. Could show me what's my fault please?
# include <stdio.h>
int main(){
int j,i,vertex_number,temp1,temp2,a=0,b=0;
printf("Vertex Number:");
scanf("%d",&vertex_number);
int graph[vertex_number];
for(i=0;i<vertex_number;i++){
scanf("%d",&graph[i]);
}
while(1){
//SORTING ARRAY
for(i=0;i<vertex_number;i++){
for(j=i+1;j<vertex_number;j++){
if(graph[i]<graph[j]){
temp1=graph[i];
graph[i]=graph[j];
graph[j]=temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]==0){
a++;
}
}
if(a==vertex_number){
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]<0){
b++;
}
}
if(b>0){
printf("graph not exist.");
return 0;
}
temp2=graph[0];
for(i=0;i<temp2;i++){
graph[i]=graph[i+1];
}
vertex_number--;
for(i=0;i<temp2;i++){
graph[i]-=1;
}
printf("-------------\n");
for(i=0;i<vertex_number;i++){
printf("%d\n",graph[i]);
}
}
}

Your have 2 issues, the exist if graph[i]-=1; is negative and the removing loop doing should be done for vertex_number and not temp2
# include <stdio.h>
int main(void) {
int i, j, vertex_number, temp1, temp2;
printf("Vertex Number:");
scanf("%d", &vertex_number);
int graph[vertex_number];
for (i = 0; i < vertex_number; i++){
scanf("%d", &graph[i]);
}
while (1) {
//SORTING ARRAY
for (i = 0; i < vertex_number; i++) {
for (j = i+1; j < vertex_number; j++) {
if (graph[i] < graph[j]) {
temp1 = graph[i];
graph[i] = graph[j];
graph[j] = temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
if (graph[0] == 0) {
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for (i = 0; i < vertex_number; i++) {
if (graph[i] < 0){
printf("graph not exist.");
return 0;
}
}
temp2 = graph[0];
vertex_number--;
for (i = 0; i < vertex_number; i++) { // HERE was your issue
graph[i] = graph[i + 1];
}
for (i = 0; i < temp2; i++) {
graph[i]-=1;
if (graph[i] < 0) {
printf("graph not exist.");
return 0;
}
}
printf("-------------\n");
for (i = 0; i < vertex_number; i++) {
printf("%d\n",graph[i]);
}
}
}
You don't need a, if the first is null all the other are null or negative
You don't need b neither just stop on first occurence

Not an answer, but a cleaned-up version that works follows.
The key is that it literally removes the first element from the array by advancing the pointer and reducing the size of the array. That way, we're always working with elements 0..s-1 or 0..n-1.
// Destroys the contents of the provided array.
int havel_hakimi(unsigned *degrees, size_t n) {
while (1) {
// Yuck
for (size_t i=0; i<n; ++i) {
for (size_t j=i+1; j<n; ++j) {
if (degrees[i] < degrees[j]) {
int temp = degrees[i];
degrees[i] = degrees[j];
degrees[j] = temp;
}
}
}
if (degrees[0] == 0)
return 1; // Has a simple graph.
// Remove first element.
unsigned s = degrees[0];
++degrees;
--n;
if (s > n)
return 0; // Invalid input!
if (degrees[s-1] == 0)
return 0; // Doesn't have a simple graph.
for (size_t i=s; i--; )
--degrees[i];
}
}
Tested using the following:
#include <stdio.h>
int main(void) {
{
unsigned degrees[] = { 6, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 1
}
{
unsigned degrees[] = { 6, 5, 5, 4, 3, 2, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 0
}
return 0;
}

Related

Finding Median of an Array

I am trying to write a C program to find the median of an array, but the task requires to not sort the array. The current code I have works, but fails when there is a repeated number. I am struggling to find a way to account for this case. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
int median_finder(int size, int* data) {
int n1, n2;
int count = 0;
for (int t = 0; t < size; t ++) {
int piv = data[t];
int higher = 0;
int lower = 0;
int median;
if (size % 2 != 0) {
for (int j = 0; j < size; j++) {
if (piv < data[j]) {
higher++;
} else if (piv > data[j]) {
lower++;
}
}
if (higher != 0 && lower == higher) {
printf("MEDIAN: %d\n", piv);
return 0;
}
} else {
//int num = 0;
for (int j = 0; j < size; j++) {
if (piv < data[j]) {
higher++;
} else if (piv > data[j]) {
lower++;
}
}
if (higher != 0 && (lower == size/2 || higher == size/2)) {
count++;
if (count == 1) {
n1 = piv;
} if (count == 2) {
n2 = piv;
}
}
} if (count == 2) {
if (n1 > n2) {
median = n2;
} else {
median = n1;
}
printf("Median: %d\n", median);
return 0;
}
}
}
int main(int argc, char** argv) {
int size = atoi(argv[1]);
argv++;
argv++;
int data[size];
for (int i = 0; i < size; i++) {
data[i] = atoi(argv[i]);
}
median_finder(size, data);
}
The median for an unsorted array with possible duplicate values a of length n is the element with the value a[i] where half of the remaining elements (n-1)/2 (rounded down) are between less than (lt) or less than and equal (lt + eq) to a[i]:
#include <assert.h>
#include <stdio.h>
int median(size_t n, int *a) {
assert(n > 0);
for(size_t i = 0; i < n; i++) {
size_t lt = 0;
size_t eq = 0;
for(size_t j = 0; j < n; j++) {
if(i == j) continue;
if(a[j] < a[i]) lt++;
else if(a[j] == a[i]) eq++;
}
if((n-1)/2 >= lt && (n-1)/2 <= lt + eq)
return a[i];
}
assert(!"BUG");
}
// tap-like
void test(size_t test, int got, int expected) {
printf("%sok %zu\n", got == expected ? "" : "not ", test);
if(got != expected) {
printf(" --\n"
" got: %d\n"
" expected: %d\n"
" ...\n", got, expected);
}
}
int main(void) {
struct {
size_t n;
int *a;
} tests[] = {
{1, (int []) {0}},
{2, (int []) {0, 1}},
{3, (int []) {-1, 0, 1}},
{4, (int []) {-1, 0, 0, 1}},
};
for(int i = 0; i < sizeof tests / sizeof *tests; i++) {
test(i+1, median(tests[i].n, tests[i].a), 0);
}
}

how to get different array in for loop every time?

#include<stdio.h>
#include<time.h>
int main(void)
{
srand(time(NULL));
int answer;
int treatment = rand() % 4;
printf("###발모제 찾기###\n\n");
int cntShowBottle = 0;
int prevCntShowBottle = 0;
int ptr[4] = { 0,0,0,0 };
int bottle[4] = { 0, 0, 0, 0 };
int isincluded = 0;
for (int i = 1; i <= 3; i++)
{
do {
cntShowBottle = rand() % 2 + 2;
} while (cntShowBottle == prevCntShowBottle);
prevCntShowBottle = cntShowBottle;
printf(" %d 번째 시도 : ", i);
for (int j = 0; j < cntShowBottle; j++)
{
int randBottle = rand() % 4;
if (bottle[randBottle] == 0)
{
bottle[randBottle] = 1;
if (randBottle == treatment)
{
isincluded = 1;
}
}
else
{
j--;
}
}
}
if (bottle[0] == ptr[0] && bottle[1] == ptr[1] && bottle[2] == ptr[2] && bottle[3] == ptr[3])
{
int bottle[4] = { 0,0,0,0 };
for (int j = 0; j < cntShowBottle; j++)
{
int randBottle = rand() % 4;
if (bottle[randBottle] == 0)
{
bottle[randBottle] = 1;
if (randBottle == treatment)
{
isincluded = 1;
}
}
else
{
j--;
}
}
}
else
{
return 0;
}
for (int i = 0; i < 4; i++)
{
ptr[i] = bottle[i];
}
for (int k = 0; k < 4; k++)
{
if (bottle[k] == 1)
printf("%d ", k + 1);
}
printf("번 물약을 머리에 바릅니다.\n\n");
if (isincluded == 1)
{
printf("성공!\n");
}
else
{
printf("실패...\n");
}
printf("\n ...계속 하려면 아무키나 누르세요...");
getchar(0);
printf("\n\n발모제는 몇 번? : ");
scanf_s("%d", &answer);
if (answer == treatment+1)
{
printf("\n 정답! \n");
}
else
{
printf("\n 실패! \n 정답은 %d 였습니다.\n", treatment+1);
}
return 0;
}
in this loop, for (int j = 0; j < cntShowBottle; j++), 'bottle' array will be [0,1,1,1] or [1,1,0,0] etc. In this loop, how to get different array without overlapping(like [0,1,1,0] and again [0,1,1,0])?? I tried comparing each elements, if it takes overlapping array, makes 'bottle' array again. but it didn't run properly. please help..

Printing patterns using arrays in c

The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888
Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}

Possible mode error

I've made this program that computes the mean, the median and the mode from an array. Although I've tested with some examples, I found out there might be a case that I have forgotten as for many of the inputs I've tested it works but the testing program that my teacher is using gave me an error for a certain test, but I was not presented with its input. Maybe someone can have a look and see if I am making a mistake at the mode point of the code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
int main(int argc, char *argv[]) {
int n, i;
scanf("%d", &n);
int *array = safeMalloc(n * sizeof(int));
for (i = 0; i < n; i++) {
int value;
scanf("%d", &value);
array[i] = value;
}
//mean
double mean;
double sum = 0;
for (i = 0; i < n; i++) {
sum = sum + (double)array[i];
}
mean = sum / n;
printf("mean: %.2f\n", mean);
//median
float temp;
int j;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++) {
if (array[i] > array[j]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
printf("median: %d\n", array[n / 2]);
//mode
int val = array[0], noOfRepetitions = 1, valMax = array[0], maxRepetitions = 1, possibleMax = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
noOfRepetitions++;
}
if (array[i] != val) {
val = array[i];
noOfRepetitions = 1;
}
if (noOfRepetitions == possibleMax) {
maxRepetitions = 1;
continue;
}
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
possibleMax = maxRepetitions;
}
}
if (maxRepetitions > 1) {
printf("mode: %d\n", valMax);
} else {
printf("mode: NONE\n");
}
return 0;
}
My idea for mode was because the numbers are sorted when just transverse it. If the next element is the same as the previous one, increase the noOfRepetitions. If the noOfRepetition is bigger than the maxRepetitions until now, replace with that. Also store the last maximum val needed if we have for example more than 2 numbers with the same number of repetitions.
EDIT: The mode of an array should return the number with the maximum number of occurrences in the array.If we have 2 or more number with the same number of maximum occurrences , there isn't a mode on that array.
I've discovered my mistake. I didn't think of the case when I have numbers with same maximum frequency and after that came one with lower frequency but still bigger than others. For example : 1 1 1 2 2 2 3 3 4 5 6.With my code , the result would have been 3 . I just needed to change the comparison of noOfRepetitions with oldMaxRepetition.
There seems to be no purpose for the variable possibleMax. You should just remove these lines:
if(noOfRepetitions==possibleMax){
maxRepetitions=1;
continue;
}
They cause maxRepetitions to be reset erroneously.
You could detect if the distribution is multimodal and print all mode values:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
int main(int argc, char *argv[]) {
int n, i;
if (scanf("%d", &n) != 1 || n <= 0)
return 1;
int *array = safeMalloc(n * sizeof(int));
for (i = 0; i < n; i++) {
if (scanf("%d", &array[i]) != 1)
return 1;
}
//mean
double sum = 0;
for (i = 0; i < n; i++) {
sum = sum + (double)array[i];
}
printf("mean: %.2f\n", sum / n);
//median
int j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (array[i] > array[j]) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
printf("median: %d\n", array[n / 2]);
//mode
int val = array[0], noOfRepetitions = 1, valMax = array[0], maxRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
noOfRepetitions++;
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}
} else {
val = array[i];
noOfRepetitions = 1;
}
}
if (maxRepetitions == 1) {
printf("mode: NONE\n");
} else {
printf("mode: %d", valMax);
val = array[0];
noOfRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
noOfRepetitions++;
} else {
if (noOfRepetition == maxRepetitions && val != valMax) {
printf(", %d", val);
}
val = array[i];
noOfRepetitions = 1;
}
}
if (noOfRepetition == maxRepetitions && val != valMax) {
printf(", %d", val);
}
printf("\n");
}
return 0;
}
Your code to search a mode seems too complicated. Compare this:
//mode
int val = array[0], noOfRepetitions = 1,
valMax = array[0], maxRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
if (++noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}
}
else
{
val = array[i];
noOfRepetitions = 1;
}
}
It's probably the simplest code to do what you need, but it overwrites maxVal and maxRepetitions much too often.
The following version overwrites the two 'max' variables only once per each new maximum found – at the cost of duplicating some part of code:
//mode
int val = array[0], noOfRepetitions = 1,
valMax = array[0], maxRepetitions = 1;
for (i = 1; i < n; i++) {
if (array[i] == val) {
++noOfRepetitions;
}
else
{
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}
val = array[i];
noOfRepetitions = 1;
}
}
if (noOfRepetitions > maxRepetitions) {
valMax = val;
maxRepetitions = noOfRepetitions;
}

Find maximum value from 2 dimensional array & addition of all the values before the maximum value & multiply the all values after the maximum value

Here is my code
#include<stdio.h>
void main() {
int a[4][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 15, 6, 5 },
{ 4, 3, 2, 1 } };
int max = a[0][0];
int mIndexF, mIndexE, addition = 0, multiplication = 1, i, j, status = 0, k,
l;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
mIndexF = i;
mIndexE = j;
}
}
}
for (k = 0; k < 4; k++) {
for (l = 0; l < 4; l++) {
if ((a[k][l] < max) && (status == 0)) {
addition += a[k][l];
} else {
status++;
if (a[k][l] != max) {
multiplication *= a[k][l];
}
}
}
}
printf("Addition is %d\n", addition);
printf("Multiplication is %d", multiplication);
return 0;
}
I want to find the maximum value. Also want to print addition of the values which are in before of the maximum value and want to print multiply value of the values which are in after the maximum value.
The following should do the trick:
#define MAX_INT (((unsigned int)(-1))>>1)
#define MIN_INT (~(MAX_INT))
void minmax(int a[4][4])
{
int i, j, maxi=0, maxj=0, max=MIN_INT, sum=0, mul=1;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
maxi = i;
maxj = j;
}
}
}
// this is to add and multiply
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i< maxi || (i==maxi && j<maxj)) // this is "before"
sum += a[i][j];
else if (i==maxi && j==maxj) // this is "same"
; //..nothing to do
else mul *= a[i][j];
}
}
printf("i,j=%d,%d; sum= %d, mul= %d\n", maxi, maxj, sum, mul);
}
EDIT: Added definitions of MAX_INT and MIN_INT
Your code seems to be correct: just initialize
max=INT_MIN using include<limits.h>
Your second loops seems to slight inappropriate you could use:
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(status==0)
add+=disp[i][j];
else if(status==1)
mul*=disp[i][j];
if(i==loc_i && j==loc_j)
status=1;
}
Afterwards just subtract
add-=disp[mIndexF][mIndexE];
#include<stdio.h>
void main(){
int a[4][4]={
{10,11,12,13},
{14,15,16,17},
{18,19,20,21},
{22,2,3,3}
};
int max = a[0][0],mIndexF,mIndexE,addition = 0,multiplication = 1,i,j,status=0,k,l;
// this is for find out maximum value
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(max<a[i][j]){
max = a[i][j];
mIndexF=i;
mIndexE=j;
}
}
}
printf("The maximum value is %d\n", max);
for(k=0;k<4;k++){
for(l=0;l<4;l++){
if((a[k][l]<max) &&(status==0)){
addition+=a[k][l];
}else{
status++;
if(a[k][l]!=max){
multiplication*=a[k][l];
}
}
}
}
printf("Addition is %d\n",addition);
printf("Multiplication is %d",multiplication);
return 0;
}

Resources