473,408 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

array program that needs help

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
2 1344
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
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

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

Similar topics

6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
8
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in...
26
by: Protoman | last post by:
How would you use a lookup table of char*, I mean, how would you find out where the intersection of D and E is? And how would you read two strings, and parse each character. Here's the array: ...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
3
by: mintominto82 | last post by:
Hello, I am a newbie to C++. I read C++ Primer from cover to cover at least 3 times, but that did not help. I am trying to write a program that loads txt file and write the data into array. the data...
6
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
Hey guys, I've searched high and low for a way to populate a vb array with data from a javascript array. I can find 50 ways to do it with ASP but not one for VB. I appreciate what help you can...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.