dimanche 29 mars 2015

GCD logical error



I wrote this code to get the gcd in c. It works perfectly for small numbers but when I tried 60 and 90 for example I got 2, while getting 3 for 9 &6.



#include "stdio.h"
#include "stdlib.h"
int main()
{
int a,b,x,gcd;
printf("Please Enter the fraction");
scanf("%d", &a);
scanf("%d", &b);
if(b%a == 0)
gcd =a;
else
{
x=a;
while(--x>1)
{
if (a%x == 0 && b%x==0)
{
gcd =x;
}
}
}
printf("GCD = %d", gcd);
return 0;
}



Read an array from a file until 0 is encountered



The array number of elements is unknown


The file input is: 2 3 4 5 8 0


The array will have: 2 3 4 5 8



int read(FILE *in,int a[])
{
int i = 0;
int temp;
while(fscanf(in, "%d", &temp) != EOF)
{
a[i++] = temp;
}

return i;
}


Now I've got troubles now printing it out to a file this code shows nothing:



void printingg(FILE *out,int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
fprintf(out,"%d ",a[i]);
}
}



Linked List in c add to front



So I'm really confused. I am trying to write a c method that allows me to add a new "node" to the front of a linked list. I have done this before in c++ and no problem. I am getting frustrated because after writing code I was pretty sure was right I went and looked around and basically everywhere I find tells me to do the same thing I am all ready doing...I'll provide code and step by step addresses and values of the variables. Here is the code:


The real function in question is:



void addToBeginning(int value, struct node* root){
struct node* newNode;
newNode = malloc( sizeof(struct node));
newNode->value = value;
newNode->next = root;
root = newNode;
}


But here is the complete code. I removed some things to make it more concise (things that aren't necessary to answer this question like getLength(...) and addToPos(...))



#include <stdio.h>
#include <stdlib.h>

struct node {
int value;
struct node* next;
};

void printLinkedList(struct node* root);

void addToEnd(int value, struct node* root);

void addToBeginning(int value, struct node* root);

void addToPos(int pos, int value, struct node* root);

int getLength(struct node* root);


int main(){
/**
TESTING addToBeginning (I know addToEnd works)
**/

struct node *root2;
root2 = malloc( sizeof(struct node));
root2->value = 4;
root2->next = NULL;

/*
root2 = 4 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0
*/
i = 0;
while (i < 5){
addToEnd(0,root2);
i++;
}

printLinkedList(root2);
//printf("Length : %d\n",getLength(root2));

/*
expected root2 = 2 -> 4 -> 0 -> 0 -> 0 -> 0 -> 0
*/
addToBeginning(2, root2);
printLinkedList(root2);
/*
obtained root2 = 4 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0
*/
//printf("Length : %d\n",getLength(root2));

return(0);
}

void printLinkedList(struct node* root){
while(root != NULL){
if (root->next != NULL){
printf("%d, ",root->value);
root=root->next;
} else {
printf("%d\n",root->value);
root=root->next;
}
}

}

void addToEnd(int value, struct node* root){
/*
Set up new node
*/
struct node* newNode = malloc( sizeof(struct node));
newNode->value = value;
newNode->next = NULL;

/*
Check if empty linked list first
*/
if (root->next == NULL){
root->next = newNode;
} else {
/*
Find the last node
*/
struct node* current = root;
while(current->next != NULL){
current = current->next;
}
current->next = newNode;
}
}

void addToBeginning(int value, struct node* root){
struct node* newNode;
newNode = malloc( sizeof(struct node));
newNode->value = value;
newNode->next = root;
root = newNode;
}


What is so confusing is I feel that there is a problem with aliasing at the



newNode->next = root;
root = newNode;


lines...so I am including the addresses obtained during my debug steps: So...within the addToBeginning(int value, struct node* root){...} function I'm going to go through step by step:


after execution of:



struct node* newNode;
newNode = malloc( sizeof(struct node));


the addresses and values of root and newNode are:



root = 0x0000000100103c60
root->next = 0x0000000100103c70
root->value = 4
newNode = 0x0000000100103cc0
newNode->next = NULL
newNode->value = 0


after execution of:



newNode->value = value;


the addresses and values of root and newNode are:



root = 0x0000000100103c60
root->next = 0x0000000100103c70
root->value = 4
newNode = 0x0000000100103cc0
newNode->next = NULL
newNode->value = 2


after execution of:



newNode->next = root;


the addresses and values of root and newNode are:



root = 0x0000000100103c60
root->next = 0x0000000100103c70
root->value = 4
newNode = 0x0000000100103cc0
newNode->next = 0x0000000100103c60
newNode->value = 2


after execution of:



root = newNode;


the addresses and values of root and newNode are:



root = 0x0000000100103cc0
root->next = 0x0000000100103c60
root->value = 2
newNode = 0x0000000100103cc0
newNode->next = 0x0000000100103c60
newNode->value = 2


I realize that the problem is that *root is passed by reference so what I need to do is change the value of the object stored in the location 0x0000000100103c60 so any suggestions on how to do that would be appreciated.




Permuting bottom element with top element in a stack




#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct node
{
int data;
struct node *next;
};
struct node *top;


int count=0;
void push(int n);
void Print();
void permute();

int main()
{
int no, ch, e;
printf("\n1 - Push");
printf("\n4 - Print");
printf("\n7 - Permute first and last element");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 4:
Print();
break;

case 7:
permute();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}


}
void push(int no)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=no;
temp->next=top;
top=temp;
count++;
}
void Print()
{
struct node *temp=top;
printf("List is:");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void permute()
{
int i;
struct node *temp=(struct node*)malloc(sizeof(struct node));
struct node *temp1=(struct node*)malloc(sizeof(struct node));
struct node *temp2=(struct node*)malloc(sizeof(struct node));
temp=top;
temp1=NULL;
for(i=0;i<count-1;i++)
{
temp1=temp1->next;
}
temp1->next=temp2;
temp2->data=temp1->next->data;
temp1->next=temp;
temp->data=top->data;
temp=NULL;
temp2->next=top;
top=temp2;
}


So my implementation of a stack works fine as for pushing and printing the elements in the stack, but when I want to permute the bottom element with the top element the program crashes. I think I am messing something up in my permute function. Thank you for any help before hand.




Why does this program print 25 instead of 27 if I input 5? [duplicate]




This question already has an answer here:





int main(void)
{

int i;
scanf( "%d", &i );
i = i++ * i++ ;
printf( "%d", i );
getchar();
getchar();
return 0;

}


Why does this program print 25 instead of 27 if I input 5 ?




How can I track system call in win32 API program with debugger(VS 2013)?



Well, I wrote a code for File I/O with Win32 API. (I'm using Visual studio 2013)


It just gets two file name(one for source, one for destination) and duplicate one to another.


I used CreateFile, ReadFile, WriteFile.


It's functionally simple. It's not problem. But..


I wanna SEE the system call in these function being called in debugger.


How can I do this?


with Call stack? Disassembler?




GtkStatusBar gets stuck at a /random/ position and wants me to mousehover button



I will keep it simple


I have the function inside_thread that runs a while loop and calls the function update_progressbar that simply updates a GtkStatusBar.


I am calling inside_thread in a thread using g_thread_new("processing", GThreadFunc, NULL); from a callback function on_starter_clicked.




It tends to do the job, it works fast and it updates the status bar according to the progress of the while loop.


The problem is that sometimes (at unspecified place) the progress bar gets stuck and it gets unstuck if I mouseenter or mouseleave a button in the application. Very strange behavior I must admit


What could be wrong?