I need some help with my program..

This is a discussion on I need some help with my program.. within the C++ Programming forums, part of the General Programming Boards category; Edit: So, seems I do have a problem somewhere in my coding. Whenever I run the program and input a ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    I need some help with my program..

    Edit: So, seems I do have a problem somewhere in my coding. Whenever I run the program and input a variable, it always returns the same answer.."The content at location 76 is 0. I'm trying to figure out how to incorporate this idea:


    const int OP_LOAD = 3;
    const int OP_STORE = 4;
    const int OP_ADD = 5;
    ...

    const int OP_LOCATION_MULTIPLIER = 100;

    mem[0] = OP_LOAD * OP_LOCATION_MULTIPLIER + ...;
    mem[1] = OP_ADD * OP_LOCATION_MULTIPLIER + ...;

    operand = memory[ j ] % OP_LOCATION_MULTIPLIER;
    operation = memory[ j ] / OP_LOCATION_MULTIPLIER;



    Okay guys, I posted on here a few days ago about a question but it was just a compilation error, so if this looks familiar, thats why. I will reiterate, I'm new to programming, I'm not the best, so I'm going for simplicity. Also this is an SML program. Anyway, this IS a homework assignment and I'm wanting a good grade on this. So I was looking for input and making sure this program will do what I'm hoping they are looking for. Anyway, here are the instructions: Write SML (Simpletron Machine language) programs to accomplish each of the following task:

    A) Use a sentinel-controlled loop to read positive number s and compute and print their sum. Terminate input when a neg number is entered. B) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute + print the avg. C) Read a series of numbers, and determine and print the largest number. The first number read indicates how many numbers should be processed.

    Without further a due, here is my program. All together.

    Code:
    int main()
    {
    	const int READ = 10;
    	const int WRITE = 11;
    	const int LOAD = 20;
    	const int STORE = 21;
    	const int ADD = 30;
    	const int SUBTRACT = 31;
    	const int DIVIDE = 32;
    	const int MULTIPLY = 33;
    	const int BRANCH = 40;
    	const int BRANCHNEG = 41;
    	const int BRANCHZERO = 41;
    	const int HALT = 43;
    
    	int mem[100] = {0}; //Making it 100, since simpletron contains a 100 word mem.
    	
    	int operation; //taking the rest of these variables straight out of the book seeing as how they were italisized.
    	
    	int operand;
    	
    	int accum = 0; // the special register is starting at 0
    	
    	int j;
    	
    	
    	
    	
    	// This is for part a, it will take in positive variables in a sent-controlled loop and compute + print their sum. Variables from example in text.
    	memory [00] = 1010;
    	
    	memory [01] = 2009;
    	
    	memory [02] = 3008;
    	
    	memory [03] = 2109;
    	
    	memory [04] = 1109;
    	
    	memory [05] = 4300;
    	
    	memory [06] = 1009;
    
    	j = 0; //Makes the variable j start at 0.
    
    	while ( true )
    	{
    		
    		operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
    		operation = memory[ j ]/100;
    
    		//using a switch loop to set up the loops for the cases
    		switch ( operation ){
    			case 1: //reads a variable into a word from loc. Enter in -1 to exit
    				cout <<"\n Input a positive variable:  ";
    				cin >> memory[ operand ]; break;
    		
    			case 2: // takes a word from location
    				cout << "\n\nThe content at location " << operand   << "is " << memory[operand]; break;
    		
    			case 3:// loads
    				accum = memory[ operand ]; break;
    
    			case 4: //stores
    				memory[ operand ] = accum; break;
    
    			case 5: //adds
    				accum += mem[operand]; break;
    			
    
    			case 6: // subtracts
    				accum-= memory[ operand ]; break;
    
    			case 7: //divides
    				accum /=(memory[ operand ]); break;
    
    			case 8: // multiplies
    				accum*= memory [ operand ]; break;
    
    			case 9: // Branches to location
    				j = -1; break;
    
    			case 10: //branches if acc. is < 0
    				if (accum < 0)
    				j = 5; 
    				break;
    			
    			case 11: //branches if acc = 0
    				if (accum == 0)
    					j = 5; 
    				break;
    
    			case 12: // Program ends
    				exit(0); break;
    	}
    	j++;
    	}
    return 0;
    }
    
    //Part b finding the sum + avg.
    
    int main()
    {
    	const int READ = 10;
    	const int WRITE = 11;
    	const int LOAD = 20;
    	const int STORE = 21;
    	const int ADD = 30;
    	const int SUBTRACT = 31;
    	const int DIVIDE = 32;
    	const int MULTIPLY = 33;
    	const int BRANCH = 40;
    	const int BRANCHNEG = 41;
    	const int BRANCHZERO = 41;
    	const int HALT = 43;
    
    	int mem[100] = {0};
    	int operation;
    	int operand;
    	int accum = 0;
    	int pos = 0;
    
    	int j;
    	
    
    	mem[22] = 7; // loop 7 times
    	mem[25] = 1; // increment by 1
    
    	mem[00] = 4306;
    	
    	mem[01] = 2303;
    	
    	mem[02] = 3402;
    	
    	mem[03] = 6410;
    	
    	mem[04] = 3412;
    	
    	mem[05] = 2111;
    
    	mem[06] = 2002;
    	
    	mem[07] = 2312;
    	
    	mem[08] = 4210;
    	
    	mem[09] = 2109;
    	
    	mem[10] = 4001;
    
    	mem[11] = 2015;
    	
    	mem[12] = 3212;
    	
    	mem[13] = 2116;
    
    	mem[14] = 1101;
    	
    	mem[15] = 1116;
    
    	mem[16] = 4300;
    
    	j = 0;
    
    	while ( true )
    	{
    		
    		operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
    		operation = memory[ j ]/100;
    
    		//using a switch loop to set up the loops for the cases
    		switch ( operation ){
    			case 1: //reads a variable into a word from loc. Enter in -1 to exit
    				cout <<"\n enter #:  ";
    				cin >> memory[ operand ]; break;
    		
    			case 2: // takes a word from location
    				cout << "\n\nThe content at location " << operand   << "is " << memory[operand]; break;
    		
    			case 3:// loads
    				accum = memory[ operand ]; break;
    
    			case 4: //stores
    				memory[ operand ] = accum; break;
    
    			case 5: //adds
    				accum += mem[operand];; break;
    			
    
    			case 6: // subtracts
    				accum-= memory[ operand ]; break;
    
    			case 7: //divides
    				accum /=(memory[ operand ]); break;
    
    			case 8: // multiplies
    				accum*= memory [ operand ]; break;
    
    			case 9: // Branches to location
    				j = operand; break;
    
    			case 10: //branches if acc. is < 0
    				
    				break;
    			
    			case 11: //branches if acc = 0
    				if (accum == 0)
    					j = operand; 
    				break;
    
    			case 12: // Program ends
    				exit(0); break;
    	}
    	j++;
    	}
    return 0;
    }
    
    
    
    ///Part c
    int main()
    {
    	const int READ = 10;
    	const int WRITE = 11;
    	const int LOAD = 20;
    	const int STORE = 21;
    	const int ADD = 30;
    	const int SUBTRACT = 31;
    	const int DIVIDE = 32;
    	const int MULTIPLY = 33;
    	const int BRANCH = 40;
    	const int BRANCHNEG = 41;
    	const int BRANCHZERO = 41;
    	const int HALT = 43;
    
    	int mem[100] = {0};
    	int operation;
    	int operand;
    	int accum = 0;
    
    
    	int j;
    	
    	
    	mem[23] = 1; //decrements 1 place in mem
    	
    	mem[0] = 1030; // Takes in # of values to be stored.
    	
    	mem[01] = 4123; // These 4 memory slots check for the largest variable then store
    	mem[02] = 4134;
    	mem[03] = 1011;
    	mem[04] = 3204;
    
    	mem[05] = 4005; // These 5 decrement the count+ store + branch.
    	mem[06] = 4006;
    	mem[07] = 4007;
    	mem[08] = 4008;
    	mem[09] = 4009;
    
    	mem[10] = 4010;
    	mem[11] = 4311; // exits
    
    	j = 0; // this is the starting value..
    
    	while ( true )
    	{
    		
    		operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
    		operation = memory[ j ]/100;
    
    		//using a switch loop to set up the loops for the cases
    		switch ( operation ){
    			case 1: //reads a variable into a word from loc. Enter in -1 to exit
    				cout <<"\n enter #:  ";
    				cin >> memory[ operand ]; break;
    		
    			case 2: // takes a word from location
    				cout << "\n\nThe content at location " << operand   << "is " << memory[operand]; break;
    		
    			case 3:// loads
    				accum = memory[ operand ]; break;
    
    			case 4: //stores
    				memory[ operand ] = accum; break;
    
    			case 5: //adds
    				accum += mem[operand]; break;
    			
    
    			case 6: // subtracts
    				accum-= memory[ operand ]; break;
    
    			case 7: //divides
    				accum /=(memory[ operand ]); break;
    
    			case 8: // multiplies
    				accum*= memory [ operand ]; break;
    
    			case 9: // Branches to location
    				j = operand; break;
    
    			case 10: //branches if acc. is < 0
    				
    				break;
    			
    			case 11: //branches if acc = 0
    				if (accum == 0)
    					j = operand; 
    				break;
    
    			case 12: // Program ends
    				exit(0); break;
    			case 13: // checks > than
    				if (accum < mem[operand])
    					accum = mem[operand];
    				break;
    		}
    	j++;
    	}
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Which program are you running, a, b or c? Part a looks like it will go into an infinite loop.

    Also, remember that 08 and 8 are not the same thing in C++. 08 isn't really a valid number, I'm not sure what the compiler will do with it. The initial 0 indicates that you're using an octal number, and octal digits go from 0 to 7 only. I'd get rid of the initial 0 in those numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


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