Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:05:13+00:00 2026-06-14T21:05:13+00:00

I’m trying to get the following output from the given array Apples 200 Grapes

  • 0

I’m trying to get the following output from the given array

 Apples      200   Grapes      900 Bananas  Out of stock
 Grapefruits 2     Blueberries 100 Orangess Coming soon
 Pears       10000

Here’s what I came up so far (feels like I’m overdoing it), however, I’m still missing something when padding the columns. I’m open to any suggestions on how to approach this.

#include <stdio.h>
#include <string.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
char *fruits[][2] = {
    {"Apples", "200"},
    {"Grapes", "900"},
    {"Bananas", "Out of stock"},
    {"Grapefruits", "2"},
    {"Blueberries", "100"},
    {"Oranges", "Coming soon"},
    {"Pears", "10000"},
};

int get_max (int j, int y) {
    int n = ARRAY_SIZE(fruits), width = 0, i;
    for (i = 0; i < n; i++) {
        if (i % j == 0 && strlen(fruits[i][y]) > width) {
            width = strlen(fruits[i][y]);
        }
    }
    return width;
}

int main(void) {
    int n = ARRAY_SIZE(fruits), i, j;
    for (i = 0, j = 1; i < n; i++) {
        if (i > 0 && i % 3 == 0) {
            printf("\n"); j++;
        }
        printf("%-*s ", get_max(j, 0), fruits[i][0]);
        printf("%-*s ", get_max(j, 1), fruits[i][1]);
    }
    printf("\n"); 
    return 0;
}

Current output:

Apples      200          Grapes      900          Bananas     Out of stock 
Grapefruits 2            Blueberries 100          Oranges     Coming soon  
Pears       10000 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:05:15+00:00Added an answer on June 14, 2026 at 9:05 pm

    You are computing widths wrong. In essence, you want to be able to compute the width of a particular column. Thus, in your get_max function, you should be able to specify a column. We can then pick out the elements from the list based on whether their index mod 3 is equal to the column. This can be accomplished as such:

    int get_max (int column, int y) {
        ...
            if (i % 3 == column /* <- change */ && strlen(fruits[i][y]) > width) {
        ...
    }
    

    Then in your main loop, you want to choose the widths of the columns based on what column you are currently in. You can do that by taking the index mod 3:

    for (i = 0, j = 1; i < n; i++) {
        ...
        printf("%-*s ", get_max(i % 3 /* change */, 0), fruits[i][0]);
        printf("%-*s ", get_max(i % 3 /* change */, 1), fruits[i][1]);
    }
    

    This should work as you expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.