By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

array program that needs help

P: 43
I'm a first year programmer and Im just learning the basics...I have this program so far but all it does is take 20 numbers and print them out I need to figure out how to take the input and print them out with no repeats...any ideas??

Heres the program:
1. Accountant. Write a program that accepts a sequence of integers (some of which may repeat) as input
and prints every integer exactly once. You can assume that there are no more than 20 integers in the input.
But there may be less. Zero signifies the end of input. It should NOT be printed.

For example, when the following input is provided to your program:

5 6 22 5 22 7 6 0

your program should print:

5 6 22 7

heres what i got:

#include <iostream>
using namespace std;
int main()
{
int array[20];
int x =0;
for(int x=0; x<20; x++)
cin>>array[x];
for(x=0; x<20; x++)
cout << array[x] << endl;
return 0;
}
Nov 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: 43
I think this is more on the right track but now im stuck again I dont know how to get it to print out my array without the repeated numbers.
Code:
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 20;
int array[SIZE];
int i =0, length;
do
{
cin >> array[i];
i++;
}
while (array[i-1] != 0 && i < SIZE);
Nov 2 '06 #2

P: 2
While I am also a beginner in programming, I think you might want to try using an "if" statement in your "for" loop.

this might be confusing, but I had to do something similiar...


Here is my version...you have to enter numbers (positve, negative, or zero) and count them...hope this helps..
good luck

#include <stdio.h>
#include <math.h>

int main ()

{ /*main*/


float*value=(float*)NULL;


const int minimum_number_of_values=1;
const int program_fail_code =-1;
const int first_index=0;
const int program_success_code;
int number_of_values, index;
int number_of_zeros=0;
int number_of_positives=0;
int number_of_negatives=0;
printf("This is a trivial question, but this");
printf(" program counts postive, negative, and zero values.\n");
printf("How many values do you want to input?\n");
scanf("%d", &number_of_values);
if (number_of_values<minimum_number_of_values){
printf("ERROR! value not recognized.\n");
exit(program_fail_code);
} /* if (number_of_values<minimum_number_of_values) */
value=(float*)malloc(sizeof(float)*number_of_value s);
if(value==(float*)NULL){
printf("ERROR! Allocation failed.\n");
exit(program_fail_code);
} /* if(value==(float*)NULL) */
printf("Please enter %d values.\n", number_of_values);
for(index = first_index; index < number_of_values; index++){
scanf("%f", &value[index]);
} /* for(index=first_index; index<number_of_values, index++)*/
for(index = first_index; index < number_of_values; index++){
if (value[index] > 0){
number_of_positives++;
} /*if (value[index] > 0) */
} /* for(index=first_index; index<number_of_values, index++)*/
for(index = first_index; index < number_of_values; index++){
if(value[index] < 0){
number_of_negatives++;
} /* for(index=first_index; index<number_of_values, index++)*/
} /*(value[index] < 0) */
for(index = first_index; index < number_of_values; index++){
if(value[index] == 0){
number_of_zeros++;
} /* if(value[index] == 0) */
} /* for(index=first_index; index<number_of_values, index++)*/
printf("The number of positive numbers is %d.\n", number_of_positives);
printf("The number of negative numbers is %d.\n", number_of_negatives);
printf("The number of zeros is %d.\n", number_of_zeros);
free(value);
value = (float*)NULL;
return program_success_code;
} /* main */
Nov 3 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.