C :: Ignore Negative Numbers In Array
Feb 15, 2014
So i have this program that takes in user input and stores them into an array and then prints them, removes duplicates, and sorts in ascending order. The user can also stop by inputting a sentinel value (in this case -1). But i am also supposed to ignore any negative value besides -1. When i input any other negative value into the program it messes up. How would i go about ignoring the negative values?
Code:
#include<stdio.h>
int main()
{
int input, nums[20], i, j, k, temp, count=0, count2=0;
for(i=0;i<20;i++)
[Code] .....
View 8 Replies
ADVERTISEMENT
Sep 20, 2013
I want to write a c++ code, which will read in a file like this:
A: 10
B: 20
C: 30
while in the code, I would need the value of A B C, how can I let the cin ignore the labels like A:?
of course, I can write a file as
10
20
30
.
.
.
and use cin>>A>>B>>C; to read in those
but in this way, when the number of values increase, it is really hard to remember who is who?
View 5 Replies
View Related
May 15, 2013
ignoring negative numbers when I am trying to add up only positive numbers.
SAMPLE:
if (num>=0) {
sum= sum + num;
}
else
how would the else in this case being a negative number not be included in the sum
View 4 Replies
View Related
Jan 22, 2015
I have a program where the user inputs a line of numbers, and the two highest ones are displayed. It works fine, until negative values are entered at which point it shows 0 as the result.
Code: #include <iostream>
using namespace std;
int main( ) {
int num = 0;
int highest = 0;
[Code].....
View 6 Replies
View Related
Jul 24, 2013
I've been working on this program to create a simple desk calculator for a school assignment, and I managed to finish. All we had to do was add, subtract, multiply, and divide positive integers - and I was able to do that just fine. This program got me thinking though, because I do not know how to write commands to multiply/divide negative numbers.
In fact, when I divide a number like 21 by 4, it comes out to 5 because I don't know how to allow it to compute remainders (which wasn't a requirement for my program). This intrigued me so I've been trying to figure it out for the last few days but to no avail. Here's my code:
Code: void flush_buffer(){
int ch;
while ((ch = getchar()) != '
' && ch != EOF);
[Code]....
And just know that my code works perfectly fine, I'm not here for troubleshooting it. I just want to know what I can change to allow negative values to be correctly computed.
View 12 Replies
View Related
Sep 2, 2013
I have a homework assignment due that told me for the "input specification" that "n" is an integer greater then 0. How would I put this in and where?
View 1 Replies
View Related
Mar 2, 2014
I am reading one of the exercise solutions for C Programming Language: The C Programming Language Exercise 3-4
In it, it states that negative numbers are biased by (2^n - 1) (i.e. -I is represented by (2^n - 1) - (+I). So:
Code:
Bias = 2^8 - 1 = 255 = 11111111
Subtract 25 = 00011001
Equals = 11100110
what is meant by the "bias" here and what is the value of "I" here. It just suddenly uses "I" without explaining what it is.
View 4 Replies
View Related
Jan 27, 2014
#include <iostream>
using namespace std;
int initialization (int []);
int identifying (int [],int);
[Code].....
View 8 Replies
View Related
Jul 28, 2012
Ok well i know that isdigit in my code is seeing the negatives as a character and so i made my program give an error message. But i need negative numbers to work how could i get this to work with negative numbers.
The code is supposed to add the 3 numbers input into the command line argument including negative numbers.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[]){
int add=0;
int i;
[Code] ...
View 2 Replies
View Related
Oct 21, 2013
I have a error with one of my programs. I'm supposed to get rid of negative numbers when there are numbers that are randomly generated. Here is the middle part of the code.
{
int vectorLength = 10;
vector<int> bothSigns(vectorLength);
cout << " Input vector: ";
for (int i = 0; i < vectorLength; i = i + 1)
{ bothSigns[i] = rand()%201 - 100;
[code] .....
The part where i'm supposed to start is after the /////'s. However, whenever I input a number for the random numbers(not put in part of code), i keep getting a segmentation error.
View 3 Replies
View Related
Mar 6, 2013
I have to write a program that uses negative numbers in a conjecture algorithm. The program terminates when a number is reached that was already outputted. I can not get it to work and I have been racking my brain for a week.
#include <iostream>
using namespace std;
int main() {
int x[1000], y[1000], i=0, j=0, count=0, w=0, z[1000], t = 0;
[code]....
View 2 Replies
View Related
Mar 1, 2014
I having some issues with two different programs here... One of them crashes and returns random negative numbers whenever it reaches a "fscanf" function and the other displays a "Polink fatal error: access denied" error.
Code:
/* Evan Wentz */
/* Pike - ET2560 */
#include <stdio.h>
#define MAX_ACCTS 100
int accounts [];
double balances [];
[Code] ....
I thought the reason this kept crashing before was because I didn't type the data into the text file it was writing too correctly, but I made another program to do that, and it crashed whenever it got to fprintf. Program works perfect besides the file stuff...
View 6 Replies
View Related
Nov 25, 2014
I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....
Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;
[Code] ....
View 6 Replies
View Related
Nov 12, 2014
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
[Code] ....
This program was made to allow students to enter as many test scores as they want and the program will show them in ascending order and then will calculate the average of all test scores. It works wonderful until you enter a negative test score and then it throws the averaging process off. I can't seem to be able to make the program not accept the negative numbers.
View 1 Replies
View Related
Jun 19, 2014
So I have to make a program that allows the user to enter both positive and negative numbers and the program is suppose to calculate the sum of only the positive values while ignoring the negative values. Also it is to be a sentinel-controlled loop with a number ending the set of values.
View 4 Replies
View Related
Aug 27, 2014
Having error . I multiplied 0 by -4 and my result is -0 instead of 0. I tried to change the data type put It won't work. This is my code:
#include <iostream>
int main () {
double b, c;
std::cout<<"b: ";
std::cin>>b;
std::cout<<"c: ";
std::cin>>c;
std::cout<<b*c<<std::endl;
return 0;
}
View 2 Replies
View Related
Oct 14, 2014
I have an array of array with negative index. It is an array which has real dimensions [dim_y + 40][dim_x + 40] but the user uses the array like it has dimensions [dim_y][dim_x].
So i see the array's rows lets say from -20 to dim_y + 20 but a user sees only from 0 to dim_y.
First i had global and already defined the dimensions dim_x, dim_y, so i had this:
Code:
int map_boundaries[dim_y + 40][dim_x + 40];
int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20]; In fact, 'map' points to 'map_boundaries' , map[0][0] is map_boundaries[20][20].
I did what is posted in the second post here: Negative array indexing - Everything2.com
I want 'map' to be global. Until now i had defined the dim_y and dim_x so that worked fine.Now i just need to read from a user the dim_x and dim_y.
Until now i have global
Code: int **map_boundaries;
and then in main i use calloc:
Code:
map_boundaries = (int **)calloc(dim_y + 40,sizeof(int*));
for(i = 0; i < dim_y + 40; i++){
map_boundaries[i] = (int *)calloc(dim_x + 40,sizeof(int));}
but i dont know how to declare this line now:
Code: int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
View 6 Replies
View Related
Oct 14, 2014
I have an array of array with negative index. It is an array which has real dimensions [dim_y + 40][dim_x + 40] but the user uses the array like it has dimensions [dim_y][dim_x].
So I see the array's rows lets say from -20 to dim_y + 20 but a user sees only from 0 to dim_y.
First I had global and already defined the dimensions dim_x, dim_y, so i had this:
int map_boundaries[dim_y + 40][dim_x + 40];
int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
In fact, 'map' points to 'map_boundaries' , map[0][0] is map_boundaries[20][20].
I did what is posted in the second post here: [URL] ....
I want 'map' to be global. Until now i had defined the dim_y and dim_x so that worked fine. Now I just need to read from a user the dim_x and dim_y. Until now i have global
int **map_boundaries;
and then in main i use calloc:
map_boundaries = (int **)calloc(dim_y + 40,sizeof(int*));
for(i = 0; i < dim_y + 40; i++){
map_boundaries[i] = (int *)calloc(dim_x + 40,sizeof(int));
}
But I dont know how to declare this line now:
int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
View 3 Replies
View Related
Nov 8, 2014
I need to ignore negative values in an array, but not exit the function.
For example if the user enters:
1,2,3,4,-1,-2,0
the array would be
[1,2,3,4]
Is there a way to ignore these values?
View 1 Replies
View Related
Dec 11, 2013
im supposed to create a program that reads in a list of integers from the terminal and writes the negative numbers to one file and the positive numbers to another file.
i got most of it doen but for some reason its not writting the negative numbers. on what im doing wrong?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int pos_num = 0;
int neg_num = 0;
int positive_numbers = pos_num % 5;
[Code]...
View 2 Replies
View Related
Nov 18, 2013
I am trying to average the negative numbers and positive number and of course the total average.
This will read in a list of n values, where n is not known ahead of time. The number of values read into the array will be saved in n.
vector<int> readList() {
std::vector<int> result;
ifstream inFile;
inFile.open("setA.txt");
for (int x; inFile >> x; ) {
result.push_back(x);
[code]....
array is a one-dimensional array of integers and n is the number of elements in that array that contain valid data values. Both of these are input parameters to the function. The function must calculate 1) the average of the n integers in array, storing the result in ave; 2) the average of the positive numbers (> 0), storing the result in avePos, and 3) the average of the negative numbers (< 0), storing the result in aveNeg.
void avgs (std::vector &array, int &ave, int &avePos, int &aveNeg) {
int sum = 0, pos_sum = 0, neg_sum = 0, pos_count = 0, neg_count = 0;
for (auto i : array) {
sum += i;
if (i > 0) { pos_sum += i; ++pos_count; }
[code]....
View 1 Replies
View Related
Jan 11, 2014
Place the even lucky numbers in an array called evenList, the odd lucky numbers in an array called oddList, and the negative lucky numbers in an array called negList.
//So in main main i passed the array as parameter and the size;
void lucknumberlist(int favnum[], int size) {
int even = 0, odd = 0, neg = 0;
int evenArray[even];
int oddArray[odd];
int negArray[neg];
if(favnum[even] % 2 == 0) {
evenArray[even] = favnum[even];
[Code] .....
View 1 Replies
View Related
Oct 21, 2014
I have a problem with my assignment. I would like to ask how to transfer positive and negative values from array temperature to arrays positive and negative?
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int n=0, d=0, temperature[20], sum;
int positive[], negative[];
float avg;
[code]....
View 5 Replies
View Related
May 7, 2014
Here is the function I am trying to modify...
Code:
void DisplayAdminMenu() {
char ch;
system("cls");
printf("Admin Menu");
[Code] ......
Error! Must enter a number between 1 and 5
View 6 Replies
View Related
Sep 18, 2014
I am trying to cout just the first 5 characters the user enters into a string. I'm not sure how I can discard or not show the rest of the characters they try to enter after 5.
#include <iostream>
#include <string>
using namespace std;
int main() {
string random;
cout << "Enter any word." << endl;
cin >> random;
cout << random; //want to ignore everything after first 5 characters entered.
return 0;
}
View 1 Replies
View Related
Sep 7, 2012
In a rather large codebase, there is a set of recurring calls to a couple vararg functions (mainly for logging)
Code: logthis(foo, bar(), somefunction(parameter1,parameter2), somethingelse);
The number of parameters to this function is variable It has all types of variables, function calls, etc.
The code has thousands of these lines
There is now a need for a special version of the compile that will remove a lot of the code, among others is the logging.
Can I somehow #define the "logthis" name into something that'll remove all actual calls to the code. including the actual parameters/expressions to the call.
I can't afford to add #ifdef/#endif around each of the calls because there are too many of them, and because that'll cause some undisired side effects in our code conformance tests.
Code: #define logthis __noop
comes close, that removes the call, but it still causes the compiler to evaluate and validate all the parameters to the logthis function, which doesn't work because in this case, removing the logging headers also removes other members of the class that get used as part of the parameters to logthis()
if I could define logthis into // resulting in all the rest of the line being comments, but that doesn't work.
Preferably I'd like something to be portable, but I'll take a solution that only works on visual studio as well.
View 6 Replies
View Related