array program that needs help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joestevens232
    New Member
    • Oct 2006
    • 43

    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;
    }
  • joestevens232
    New Member
    • Oct 2006
    • 43

    #2
    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);

    Comment

    • jlucas134
      New Member
      • Nov 2006
      • 2

      #3
      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=(fl oat*)NULL;


      const int minimum_number_ of_values=1;
      const int program_fail_co de =-1;
      const int first_index=0;
      const int program_success _code;
      int number_of_value s, index;
      int number_of_zeros =0;
      int number_of_posit ives=0;
      int number_of_negat ives=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_valu es);
      if (number_of_valu es<minimum_numb er_of_values){
      printf("ERROR! value not recognized.\n") ;
      exit(program_fa il_code);
      } /* if (number_of_valu es<minimum_numb er_of_values) */
      value=(float*)m alloc(sizeof(fl oat)*number_of_ values);
      if(value==(floa t*)NULL){
      printf("ERROR! Allocation failed.\n");
      exit(program_fa il_code);
      } /* if(value==(floa t*)NULL) */
      printf("Please enter %d values.\n", number_of_value s);
      for(index = first_index; index < number_of_value s; index++){
      scanf("%f", &value[index]);
      } /* for(index=first _index; index<number_of _values, index++)*/
      for(index = first_index; index < number_of_value s; index++){
      if (value[index] > 0){
      number_of_posit ives++;
      } /*if (value[index] > 0) */
      } /* for(index=first _index; index<number_of _values, index++)*/
      for(index = first_index; index < number_of_value s; index++){
      if(value[index] < 0){
      number_of_negat ives++;
      } /* for(index=first _index; index<number_of _values, index++)*/
      } /*(value[index] < 0) */
      for(index = first_index; index < number_of_value s; 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_posit ives);
      printf("The number of negative numbers is %d.\n", number_of_negat ives);
      printf("The number of zeros is %d.\n", number_of_zeros );
      free(value);
      value = (float*)NULL;
      return program_success _code;
      } /* main */

      Comment

      Working...