-2

How in the world do you format this for things to line up, ive tried everything, nothing changes?!

printf("N Count\n");
printf("---- ------ \n");

for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
    count = getOccur(arr, MAX_ELEMENTS, arr[i]);
    printf("%1d %1d\n", arr[i], count);
}

I've tried tabbing, spacing, those % signs with the numbers for the last one, it wont change from this

N  Count
----- ---
       1      1
       2      1
       3      1

Driving me crazy! I dont get it! lol

EDIT WHOLE PROGRAM NEW QUESTION!

#include <stdio.h>

#define MAX_ELEMENTS 10


int getOccur(int a[], int num_elements, int value);
void printArr(int a[], int num_elements);

int main()
{
    int arr[MAX_ELEMENTS];
    int trim[MAX_ELEMENTS];
    int count, target, i;
    int j, k, temp;

    for(i = 0 ; i < MAX_ELEMENTS ; i++)
    {
        printf("Enter a variable for the array: ");
        scanf("%d", &arr[i]);
    }

    for (j = 1 ; j <= MAX_ELEMENTS-1 ; j++)
    {
        for(k = 0 ; k <= MAX_ELEMENTS-2 ; k++)
        {
            if(arr[k] > arr[k+1])
            {
                temp = arr[k];
                arr[k] = arr[k+1];
                arr[k+1] = temp;
            }
        }
    }

    printf("%4s %6s\n", " N ", "Count");
    printf("%4s %6s\n", "----", "------");

    for(i = 0 ; i < MAX_ELEMENTS ; i++)
    {
        count = getOccur(arr, MAX_ELEMENTS, arr[i]);
        printf("%3d %4d\n", arr[i], count);
    }
}

int getOccur(int a[], int tally, int value)
{
    int i;
    tally = 0;

    for( i = 0 ; i < MAX_ELEMENTS ; i++)
    {
        if (a[i] == value)
        {
            ++tally;
        }
    }

    return(tally);

}

void printArr(int a[], int amount)
{
    int i;

    for(i = 0 ; i < amount ; i++)
    {
        printf("%d ", a[i]);
    }

    printf("\n");
}    
Xaruth
  • 4,034
  • 3
  • 19
  • 26
Sherifftwinkie
  • 391
  • 1
  • 13
  • 21

2 Answers2

1
printf("%4s %6s\n", " N ", "Count");
printf("%4s %6s\n", "----", "------");

for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
  count = getOccur(arr, MAX_ELEMENTS, arr[i]);
  printf("%4d %6d\n", arr[i], count);
}    

That should line everything up.

EDIT

In response to the question in the comments, my approach would be to first find all the unique values in arr and save them to a different array (call it unique). Then you'd walk through the unique array in your loop:

for (i = 0; i < uniqueCount; i++)
{
  count = getOccur(arr, MAX_ELEMENTS, unique[i]);
  printf("%4d %6d\n", unique[i], count);
}

As for finding unique elements, the brute force method would be something like

size_t uniqueCount = 0;
int unique[MAX_SIZE];   // needs to be same size as original array in case
                        // all values are unique

for (i = 0; i < MAX_SIZE; i++)
{
  size_t j = 0;
  for (j = 0; j < uniqueCount; j++)
    if (arr[i] == unique[j])
      break;

  if (j == uniqueCount)
    unique[uniqueCount++] = arr[i];
}

For each element in the original array, we scan the (initially empty) unique array. If we don't find the value of a[i] in the unique array, we add it and increment uniqueCount.

Note that this method is pretty inefficient and will perform poorly as MAX_ELEMENTS gets large. Better solutions are available, but you sound like you're still at the bottom of the learning curve, so I'll leave it at that.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • @Sherifftwinkie: sure, what? – John Bode Feb 14 '13 at 18:55
  • Alright I am going to edit my OP to contain my entire program but I will ask the question here. When I run my program and do all my inputting and what not, it prints out what i inputed with their occurrences next to them. Now if i type in like "2" 6 times it will print "2" 6 times and tell me it occured 6 times, i only want it to tell me once, how do i do that?! – Sherifftwinkie Feb 14 '13 at 18:58
0

This is what you are looking for. The first column you have four - so the minimum width is four, the next column has size - so minimum width is 6 as specified in the format string. see printf manual page

printf("N    Count\n");
printf("---- ------ \n");

for(i = 0 ; i < MAX_ELEMENTS ; i++)
{
  count = getOccur(arr, MAX_ELEMENTS, arr[i]);
  printf("%4d %6d\n", arr[i], count);
}

EDIT

Edited first printf

Community
  • 1
  • 1
Ed Heal
  • 59,252
  • 17
  • 87
  • 127