Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Some trouble with my decreasing FOR loop.
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
aaydee444




PostPosted: Fri Oct 02, 2009 3:24 pm   Post subject: Some trouble with my decreasing FOR loop.

I have an assignment to convert numbers entered from a user to a different base. And I have the math part working, I know this because I spent a good portion of yesterday doing nothing but printf's.

Here's my code

c:
#include <stdio.h>

int main(void) {
    //varaible declaration
    int i=0,j=1,k=1; //counter variables
    int quotient=0; //quotient varaibel for storing the quotient
    //arrays for storing the converted values
    int convertedvalue[8]={0,0,0,0,0,0,0,0};
    int temp[8]={0,0,0,0,0,0,0,0};
    int num=500,BASE=7, remainder=0; //other varaiables used for calculations
    do{ //main loop to repeat until the inputted value is zero
        quotient=(num/BASE);//calculating the quotient
        remainder=num%BASE;//calculating the remainder
        temp[k]=remainder;
        k++; //increses counter to move over in array to store next variable
        num=quotient; //resets the new number to do calculations on next run through loop
    }
    while (num>0);//leaves when inputted value is down to zero

    for (i=k;i=0;i--){ //loops to reverse the order of the array elements       
        convertedvalue[j]=temp[i];
        j++;
    }
    return (0);
}


So basically the program compiles and runs but doesn't enter the for loop at the end. I'm pretty sure the logic is there but I'm not sure if I'm doing something that C accepts but is still wrong. The point of the For loop is to reverse the order of the array without moving any 0 values, like if the number in the 'temp' array is 00005432, i need it to be 00002345 instead. Also the 'num' variable is currently set to 500 as a test, it will be a user input. The value in the 'temp' array before the For loop is 00003131, and like i said before I need it to be 00001313.

Any help would greatly be appreciated, also thank-you
Antonio
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Fri Oct 02, 2009 4:43 pm   Post subject: Re: Some trouble with my decreasing FOR loop.

Why didn't I notice that first Doh!
I'm not absolute with C, however don't for loops use variables (like i) as private. Thus your trying to redeclare something that already exists.

I don't know what's up with C being weird. However Arrays start from 0; It wasn't working for me either however, I was running through you logic and found out this. Not sure if this was intended.



variable trace.png
 Description:
 Filesize:  34.3 KB
 Viewed:  392 Time(s)

variable trace.png


aaydee444




PostPosted: Fri Oct 02, 2009 5:09 pm   Post subject: Re: Some trouble with my decreasing FOR loop.

I see what your saying and thank-you, I had a few other errors that I now see. However, I don't think the FOR loop is executing at all, when I put a printf("blah blah blah"); it doesn't show up. Making me think that the swap that I'm trying to do isn't executing.
md




PostPosted: Fri Oct 02, 2009 10:34 pm   Post subject: RE:Some trouble with my decreasing FOR loop.

The second element of your for loop is an assignment instead of a comparison. i=0 returns 0 which is false, and so the loop never runs.

'Tis an easy mistake to make; you learn to spot it quickly Razz
aaydee444




PostPosted: Tue Oct 06, 2009 3:19 pm   Post subject: Re: Some trouble with my decreasing FOR loop.

thanks i found out i was going about my problem wrong, didn't even need the for loop in the end.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: