| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with code
1.Go round a loop in which further n numbers are read. Each posi-
tive number should be added to a variable, postot, and each negative number to negtot; 2.. Write out the postot and negtot variables. I just need help on the number added to variables part, any examples people can give? |
|
#2
|
|||
|
|||
|
The easiest way to do this would be to check wether the number is positive or negative in the loop where it's read, and add it to
the total. Some example code could be: Code:
int number;
int postot = 0;
int negtot = 0;
for (i = 0; i < n; ++i)
{
std::cin >> number;
if (number < 0)
negtot += number;
else
postot += number;
}
Good luck ![]() |
|
#3
|
|||
|
|||
|
Code:
// Assignment1.cpp :
//Joel Robinson, 200237937
//Liverpool University
//18th March 2006
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char numtimes[] = "Input the number of times you wish to loop: ";
char inpNums[] = "Input a number either smaller or large than 0......: ";
char notZero[] = "The number must not be 0!\n";
char positive[] = "The positive number will be: %i\n";
char negative[] = "The negative number will be: %i\n";
char format[] = "%d"; //format for scanf function
int number = 0;
int input;
int postot = 0;
int negtot = 0;
_asm {
// print out numtimes message
lea eax, numtimes;
push eax;
call printf;
add esp,4;
//read number value
lea eax,number;
push eax;
lea eax,format;
push eax;
call scanf;
add esp,8;
//loop for number times
mov ecx,number;
countdown:
push ecx;
//printout input numbers message
lea eax, inpNums;
push eax;
call printf;
add esp,4;
//get number from input
lea eax,input;
push eax;
lea eax,format;
push eax;
call scanf;
add esp,4;
//compare with 0
mov eax,input;
cmp eax,0;
//decide which label to jump to.
JE equal;
JL negcode;
JG poscode;
//if equal print error
equal: add esp,4;
lea eax,notZero;
push eax;
call printf;
add esp,4;
pop ecx;
jmp countdown;
// if negative add to negtot
negcode: push number;
push negtot;
add negtot,number;
add esp,8;
jmp recurse;
//else add to postot
poscode: push number;
push postot;
add postot,number;
add esp,8;
jmp recurse;
//if ecx dimished, continue, else loop
recurse: pop ecx;
loop countdown;
finish:
//print out positive message
push postot;
lea eax,positive;
push eax;
call printf;
add esp,8;
//print out negative message
push negtot;
lea eax,negative;
push eax;
call printf;
add esp,8;
}
return 0;
}
That is my code so far but I get two errors: h:\My Documents\Visual Studio Projects\Assingment1\Assingment1.cpp(87): error C2415: improper operand type on line 87 and 95 which is the parts: Code:
add postot,number; |
|
#4
|
|||
|
|||
|
It's unkown to me why you're using ASM, but i'm afraid I can't help you really, that's not my language
![]() |
|
#5
|
|||
|
|||
|
It looks like your add command expects a variable and a constant, instead of a variable and a variable. You would have to load one variable (postot) to a register, add the other variable (number) to that register, then store the register back to postot.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|