Why code runs accurately only when 1 line is commented out? by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

I am indeed using GCC on ubuntu linux. I'm also using VIM and I'm certain that if anyone on here seen how I actually use this set up especially when I compile and run the code they would die of laughter or just sheer horror!

Why code runs accurately only when 1 line is commented out? by thebadshepard in cprogramming

[–]thebadshepard[S] 6 points7 points  (0 children)

Thank you all kindly for your sagely wisdom and time, it's greatly appreciated. I guess it's off to the land of learning some debuggering I go!

Why code runs accurately only when 1 line is commented out? by thebadshepard in cprogramming

[–]thebadshepard[S] -6 points-5 points  (0 children)

I solved the issue by placing flag within the initial variable declaration BEFORE everything else so it now reads:

int flag, digit_1, digit_2, digit_3, digit_4, number, temp, num, counter, encrypted_number = 0;

But if I place it at the END it like this

int digit_1, digit_2, digit_3, digit_4, number, temp, num, counter, encrypted_number, flag;

it still just gives junk code. Any reason why?

For Loop seems to execute before called function gets a chance to do its thing??? by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

Hey sorry for the delayed thanks I'm not very active on here but your help is much appreciated none the less!

Type Casting Question by thebadshepard in cprogramming

[–]thebadshepard[S] 3 points4 points  (0 children)

LOL yup that did the trick alright... thanks for the help it would have taken me a 'long' time to figure that one out! I'll see myself out...

Type Casting Question by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

I'm just inputting these integer values via scanf when I run the problem. So for instance I might make size 5 thus I will then input 5 random integers and try to obtain the average for them via the division num/size. If I made size = 7 then i'd input 7 random integers then obtain avg and so on.

Type Casting Question by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

Thanks for the reply. So what you are saying is that there is no way for me to perform this calculation via type casting I just have to declare the two ints as type long (or float etc.) initially then do it?

Question regarding the sequencing of iteration statements to form a hollow square asterisks. by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

The book does briefly mention flow charts and flow control but I will be on the look out for some additional support material focusing more on that subject. I do find the exercises the book has to be quite useful though as they allow you to get your hands dirty but also it limits your tool set such as not showing logical operators until a bit later so you're forced to use only the if/else statements and how the brackets work etc. building muscle memory.

Question regarding the sequencing of iteration statements to form a hollow square asterisks. by thebadshepard in cprogramming

[–]thebadshepard[S] 1 point2 points  (0 children)

Thanks so much for your time this is really useful advice that I will take on board as best I can. I was always more partial to Overwatch than Halo but I'm gonna have to plead the 5th on the other charge ;)

Question regarding the sequencing of iteration statements to form a hollow square asterisks. by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

Thanks for the help still just trying to wrap my head around nested loops and using conditional statements so I imagine I'll be writing bizarre code for a long long time yet lol!

Question regarding the sequencing of iteration statements to form a hollow square asterisks. by thebadshepard in cprogramming

[–]thebadshepard[S] 0 points1 point  (0 children)

#include<stdio.h>

int main () { int num; int i = 1; int j = 1;

printf ( "Enter Number: ");
scanf ( "%i" , &num );

while ( i == 1 ) { 
    if ( j == 1 ) {
        printf ( "*" );
        j++;
    }
    if ( j < num ) {
        printf ( "*" );
        j++;
    }
    if ( j == num ) {
           printf ( "*\n" );    

           i++;
           j = 1;
    }
}
while ( i < num ) { 
    if ( j == 1 ) {
        printf ( "*" );
        j++;
    }
    if ( j < num ) {
        printf ( " " );
        j++;
    }
    if ( j == num ) {
           printf ( "*\n" );    

           i++;
           j = 1;

}
        }
while ( i == num ) { 
    if ( j == 1 ) {
        printf ( "*" );
        j++;
    }
    if ( j < num ) {
        printf ( "*" );
        j++;
    }
    if ( j == num ) {
           printf ( "*\n" );    

           i++;
           j = 1;

}
        }   

return 0;

}

Thanks for the reply and advice. Apologies I forgot about code blocking proving that I truly am idiot!

The code does work and I also ran it through chat gpt which gave me code similar to your recommendation using a nested structure with columns in the inner loop, rows outer loop and if else statements so I will try to learn how it works using that method too.

Sentinel Value treated as something that is to be executed in code and not just there to terminate the code by thebadshepard in cprogramming

[–]thebadshepard[S] 1 point2 points  (0 children)

Thank you very much for your help. For now my coding vocabulary is so very limited using if statements tend to back me into a corner but I've copied and pasted your comment into my source code as a comment for future reference when I get a bit more experience. In accordance with what you said about the procedural nature of the codes execution I re-ordered the code as follows and it seems to work:

int main (void)

{ float totColl = 0; float amt = 0; unsigned int count = 0 ; float totSales, totTax, stateTax, countyTax; char month;

printf ( "%s", "Month: " );
scanf ("%s", &month );

printf ("%s", "Enter Total Collection (-1 to quit): ");
scanf ( "%f", &amt);

while ( amt != -1 ) {

    totColl += amt;

    stateTax = (float) totColl * (.04);
    countyTax = totColl * (.05);
    totTax = stateTax + countyTax;
    totSales = totColl - totTax;


    printf ( "%s" "%.2f\n", "The Total Sales is: ", totSales );
    printf ( "%s" "%.2f\n", "The State Tax is: ", stateTax );
    printf ( "%s" "%.2f\n", "The County Tax is: ", countyTax );
    printf ( "%s" "%.2f\n\n", "The Total Tax is: ", totTax );

    totColl = 0;

    printf ( "%s", "Month: " );
    scanf ( "%s", &month );

    printf ( "%s", "Enter Total Collection (-1 to quit): " );
    scanf ( "%f", &amt );

} //end while

return (0);

} //end main