C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 21st, 2006, 08:30 AM
Violence Violence is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 2 Violence User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 9 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old March 21st, 2006, 09:50 AM
ossinator ossinator is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 40 ossinator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 11 m 12 sec
Reputation Power: 4
Send a message via ICQ to ossinator
Thumbs up

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

Reply With Quote
  #3  
Old March 22nd, 2006, 05:22 AM
Violence Violence is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 2 Violence User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 9 sec
Reputation Power: 0
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;
and the negtot part, any ideas?

Reply With Quote
  #4  
Old March 23rd, 2006, 07:00 AM
ossinator ossinator is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 40 ossinator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 11 m 12 sec
Reputation Power: 4
Send a message via ICQ to ossinator
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

Reply With Quote
  #5  
Old March 24th, 2006, 07:20 PM
genderadapter genderadapter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 2 genderadapter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 53 sec
Reputation Power: 0
Thumbs up

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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT