Thread: Real quick!!! Output prob.

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Real quick!!! Output prob.

    Hey guys I need my program to write the sorted array into an output file in addition to printing it. It prints just fine but it returns an empty output file. What am I missing? I know this is gonna be dumb/easy but Im not seeing it.

    Code:
    #include<stdio.h>
    #define MAX 10
    
    void sort(int a[], int n);
    
    int main()
    {
            int n;
            int list = 0;
            int i = 0;
            int a[MAX];
    
            FILE *outfile;
    
            outfile = fopen("output_lab3.txt", "w");
    
            for (i = 0; i < MAX; i++)
            {
                    printf("Enter Input Number:  ");
                    scanf("%d", &a[i]);
            }
            sort(a, sizeof(a) / sizeof(int));
            list = 0;
            while(list < sizeof(a) / sizeof(a[0]))
            {
                    printf("%d", a[list++]);
                    fprintf(outfile, "\n", a[list++]);
            }
            printf("\n");
            return 0;
    
            fclose(outfile);
    }
    
    int sort(int a[], int n)
    {
            int x, pass, tmp, swch;
            pass = 0;
            x = 0;
            swch = 1;
            while(swch)
            {
                    swch = 0;
                    x = 1;
                    while(x < n - pass)
                    {
                            if(a[x - 1] > a[x])
                            {
                                    swch = 1;
                                    tmp = a[x-1];
                                    a[x-1] = a[x];
                                    a[x] = tmp;
                            }
                            x++;
    
                     }
                    pass++;
            }
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    fprintf(outfile, "\n", a[list++]);
    You are just putting a newline character in the file. You forgot the %d format specifier.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    That worked but now it prints half my numbers in the out file and half in the terminal?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's because you have list++ for both. It increments list once when you print, and then again when you write to the file.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I dont understand what I would put as the returning value inside of a[] for the second one though?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There should only be one list++ anywhere in that output for loop. Figure out where it needs to be.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Alright guys I got it. Thanks a ton for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  4. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM