Array changes values somehow?

This is a discussion on Array changes values somehow? within the C Programming forums, part of the General Programming Boards category; I'm working on a HW problem, and for the life of me, i can't figure out why the array I'm ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Array changes values somehow?

    I'm working on a HW problem, and for the life of me, i can't figure out why the array I'm using is changing the values of its variables.
    I put a lot of double checking to figure out when it's happening in the code, but I don't WHY it's happening. Can anyone shed some light on this?

    Code:
    #include <stdio.h>
    
    int main() 
    {
    	double x[4];
    	char OP[4];
    	double y[4];
    	double answer;
    	int operation;
    
    
    	printf("x:  ");
    	scanf("%lf %lf %lf %lf %lf", &x[0], &x[1], &x[2], &x[3], &x[4]);
    	while (getchar() != '\n');
    	printf("2xCHECK: You entered: %.2lf %.2lf %.2lf %.2lf %.2lf\n", x[0], x[1], x[2], x[3], x[4]);
    
    	printf("OP: ");
    	scanf("%c %c %c %c %c", &OP[0], &OP[1], &OP[2], &OP[3], &OP[4]);
    	while (getchar() != '\n');
    	printf("2xCHECK: You entered: %c %c %c %c %c\n", OP[0], OP[1], OP[2], OP[3], OP[4]);
    	printf("y:  ");
    	scanf("%lf %lf %lf %lf %lf", &y[0], &y[1], &y[2], &y[3], &y[4]);
    	while (getchar() != '\n');
    	printf("2xCHECK: You entered: %.2lf %.2lf %.2lf %.2lf %.2lf\n", y[0], y[1], y[2], y[3], y[4]);
    
    	printf("\n\nONCEMORE: You entered: %.2lf %.2lf %.2lf %.2lf %.2lf\n", x[0], x[1], x[2], x[3], x[4]);
    //problem happens here. WHY??
    	
    	for(int k=0;k<5;k++)
    	{
    		if(OP[k]=='+')
    		{	
    			operation = 1;
    		}
    		else if (OP[k]=='-')
    		{
    			operation = 2;
    		}
    		else if (OP[k] =='*')
    		{
    			operation = 3;
    		}
    		else if (OP[k] =='/')
    		{
    			operation = 4;
    		}
    		else
    		{
    			operation = 0;
    		}
    
    		printf("%.2lf %c %.2lf = ", x[k], OP[k], y[k]);
    
    		switch(operation)
    		{
    			case 1:
    				answer = x[k] + y[k];
    				break;
    			case 2:
    				answer = x[k] - y[k];
    				break;
    			case 3:
    				answer = x[k] * y[k];
    				break;
    			case 4:
    				answer = x[k] / y[k];
    				break;
    			default:
    				break;
    		}
    
    		printf("%.2lf\n", answer);
    	
    	}
    	
    
     	return 0;
    }
    The program is supposed to take the first element of X and use the supplied operation, for the purpose of the example...an asterisk for multiplication, and multiply it against the 1st element of Y. It goes on to the second element...etc...

    Here's is what I'm putting into the program:
    x: 1.8 2.4 3 4.9 5.5
    OP: - * + - /
    y: 1.5 2 3.66 4.1 5

    And here is my exact output:
    Code:
    x: 1.8 2.4 3 4.9 5.5
    2xCHECK: You entered: 1.80 2.40 3.00 4.90 5.50 
    OP: - * + - /
    2xCHECK: You entered:  - * + - /
    y: 1.5 2 3.66 4.1 5
    2xCHECK: You entered: 1.50 2.00 3.66 4.10 5.00
    
    
    ONCEMORE: You entered: 5.00 2.40 3.00 4.90 0.00 
    5.00 - 1.50 = 3.50
    2.40 * 2.00 = 6.80
    3.00 + 3.66 = 6.66
    4.90 - 4.1 = 0.08
    0.00 / 5.00 = 0.00
    So as you can see, the array of X's, for whatever reason, changes it's [0] and [4] elements for NO REASON right before the for loop. It's very frustrating.
    Why is this, and how can I fix it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    hmm... it looks like you are accessing your arrays out of bounds, or perhaps you declared your arrays as having one less element than intended. The undefined behaviour that resulred can explain the strange effect that you are observing.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Right, i thought that might be it, so I added the printf statement to check if it's putting the right values in the array, also I initialize the array with 5 elements, and I'm accessing all the correct ones.
    In the beginning of the program, it even recognizes that I've put all the correct values in, with my first double check.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    >>scanf("%lf %lf %lf %lf %lf", &x[0], &x[1], &x[2], &x[3], &x[4]);
    >> scanf("%c %c %c %c %c", &OP[0], &OP[1], &OP[2], &OP[3], &OP[4]);
    >> scanf("%lf %lf %lf %lf %lf", &y[0], &y[1], &y[2], &y[3], &y[4]);
    You have only 4 elements, not 5!
    Last edited by Elysia; 02-19-2009 at 12:12 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by azrael
    Right, i thought that might be it, so I added the printf statement to check if it's putting the right values in the array, also I initialize the array with 5 elements, and I'm accessing all the correct ones.
    Say what you like, I see very clearly these three statements:
    Code:
    double x[4];
    char OP[4];
    double y[4];
    Quote Originally Posted by azrael
    In the beginning of the program, it even recognizes that I've put all the correct values in, with my first double check.
    That is a symptom of undefined behaviour: sometimes it works, sometimes it does not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    oh! DUH! Wow, ahahaha. I must've been tired when i wrote this last night. I'll go and fix this....hahaha.
    Thanks.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Okay, works great now! hahaha.

    thanks for catching that, i wasn't even looking there, since I thought the problem was happening somewhere before the for loop. Problem solved!
    Strange how it printed out all 5 values in the beginning, though...

    Now, clearly, I'm not that good at all this, do you have any tips?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by azrael
    do you have any tips?
    Break up your main function into smaller functions that do one thing and do it well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Ah, yes. I would've, but one of the specific instructions for this particular problem was to not use functions, for whatever reason.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    You notice some value changing suddenly without any of your code, you get a debugger.
    Then you set a memory breakpoint (when memory contents changes) to the address of the element of the array that gets changed unexpectedly. Run the code in the debugger, and the debugger will then stop when the breakpoint becomes true.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21