Thread: How Can I compile 3 source code in 1 program ?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    How Can I compile 3 source code in 1 program ?

    1. I'm wondering how to compile this 3 source code in 1 program using case statement coz im having difficult to make it in 1 combined program..
    i hope somebody could help me thanks ...

    example :

    [1] Sound
    [2] Fibonacci numbers
    [3] Multiplication of Matrices

    please select a number:


    1st code: Sound
    Code:
    #include <dos.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int menu(void);
    
    
    main()
    {
    while(1)
    {
    /*get selection and execute the relevant statement*/
    switch(menu())
    {
    case 1:
    {
    puts("sound the speaker 1\n");
    sound(2000);
    sleep(2);
    nosound();
    break;
    }
    case 2:
    {
    puts("sound that speaker 2\n");
    sound(4000);
    sleep(2);
    nosound();
    break;
    }
    case 3:
    {
    puts("You are quitting\n");
    exit(0);
    break;
    }
    default:
    {
    puts("Invalid menu choice\n");
    break;
    }
    }
    }
    return 0;
    }
    
    /*menu function*/
    int menu(void)
    {
    int reply;
    /*display menu options*/
    puts("Enter 1 for beep 1.\n");
    puts("Enter 2 for beep 2.\n");
    puts("Enter 3 to quit.\n");
    /*scan for user entry*/
    scanf("%d", &reply);
    
    return reply;
    }
    2nd code: Fibonacci numbers
    Code:
    #include <stdio.h>
    
    int main(void) {
        int n;        /* The number of fibonacci numbers we will print */
        int i;        /* The index of fibonacci number to be printed next */ 
        int current;  /* The value of the (i)th fibonacci number */
        int next;     /* The value of the (i+1)th fibonacci number */
        int twoaway;  /* The value of the (i+2)th fibonacci number */
    
        printf("How many Fibonacci numbers do you want to compute? ");
        scanf("%d", &n);
        if (n<=0)
           printf("The number should be positive.\n");
        else {
          printf("\n\n\tI \t Fibonacci(I) \n\t=====================\n");
          next = current = 1;
          for (i=1; i<=n; i++) {
     printf("\t%d \t   %d\n", i, current);
     twoaway = current+next;
     current = next;
     next    = twoaway;
          }
        }
    return 0;
    }
    
    /* The output from a run of this program was:
    
    How many Fibonacci numbers do you want to compute? 9
    
     I   Fibonacci(I) 
     =====================
     1     1
     2     1
     3     2
     4     3
     5     5
     6     8
     7     13
     8     21
     9     34
    
    */
    3rd Code: Multiplication of Matrices
    Code:
    #include<stdio.h> 
    #include<conio.h> 
    void main() 
    { 
    int mat1[2][2],mat2[2][2],mat3[2][2],i,j,k;
    clrscr();
    printf("************A program to find the multiplication of the given matrices***********\n\n"); 
    printf("************Enter the value for the 1st matrix************\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("\nEnter the value for %d row,%d column:",i+1,j+1); 
    scanf("%d",&mat1[i][j]); 
    } 
    } 
    printf("\n************Enter the value for the 2nd matrix************\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("\nEnter the value for %d row,%d column:",i+1,j+1); 
    scanf("%d",&mat2[i][j]); 
    } 
    } 
    printf("\n****************************The given matrices are*****************************\n\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("%d\t",mat1[i][j]); 
    } 
    printf("\n"); 
    } 
    printf("\n\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("%d\t",mat2[i][j]); 
    } 
    printf("\n"); 
    } 
    printf("\n****************************The multiplication of the matrices is*************\n\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    mat3[i][j]=0; 
    for(k=0;k<2;k++) 
    { 
    mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]); 
    } 
    } 
    } 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("%d\t",mat3[i][j]); 
    } 
    printf("\n"); 
    } 
    
    getch(); 
    getch(); 
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what compiler/IDE you are using
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you don't with 3 mains, hows it gonna know what to do if you were to start it

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    In Visual C++, you'd add the source files into the section on the left side containing the project/solution. You'd then include them and have a single "int main()" within the project. It's here that you include the other codes (removing redundant includes).

    #include "first source.c"
    #include "second source.c"
    #include <stdio.h> // only one instance needed
    #include <dos.h>
    // and so on

    This way, you can keep them separate. They can also all be merged into one single script as I tend to do (to a certain point), but you'd need to remove duplicate includes and global variable declarations (of which you lack global variables).

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    im using command line based turbo c borland
    Last edited by lord_cedrich; 12-10-2006 at 12:52 AM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Like this one

    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <dos.h>
    #include <conio.h>
    
    void sound();
    int menu();
    void fibonacci();
    void matrix();
    
    using namespace std;
    
    int main()
    {
       string repeat;
       int choice;
        
       do
       {
          cout << "[1] Sound" << endl;
          cout << "[2] Fibonacci numbers" << endl;
          cout << "[3] Multiplication of Matrices" << endl;
          cout << "[4] Exit" << endl;
          cout << endl << "Enter a number: " << endl;
          cin  >> choice;
          
          switch( choice )
          {
             case 1:  sound();
                      break;
                      
             case 2:  fibonacci();
                      break;
                      
             case 3:  matrix();
                      break;
                      
             case 4:  exit(0);
                      break;
                      
             default: cout << "Incorrect choice. Try again!" << endl;
          }
          
          cout << "Would you like to make another choice? (Yes/No)" << endl;
          cin  >> repeat;
       }
       while( repeat == "Yes" || repeat =="yes" );    
    }
    
    void sound()
    {
    while(1)
    {
    /*get selection and execute the relevant statement*/
    switch(menu())
    {
    case 1:
    {
    puts("sound the speaker 1\n");
    sound(2000);
    sleep(2);
    nosound();
    break;
    }
    case 2:
    {
    puts("sound that speaker 2\n");
    sound(4000);
    sleep(2);
    nosound();
    break;
    }
    case 3:
    {
    puts("You are quitting\n");
    return;
    break;
    }
    default:
    {
    puts("Invalid menu choice\n");
    break;
    }
    }
    }
    }
    
    /*menu function*/
    int menu()
    {
    int reply;
    /*display menu options*/
    puts("Enter 1 for beep 1.\n");
    puts("Enter 2 for beep 2.\n");
    puts("Enter 3 to quit.\n");
    /*scan for user entry*/
    scanf("%d", &reply);
    
    return reply;
    }
    
    void fibonacci()
    {
     int n;        /* The number of fibonacci numbers we will print */
        int i;        /* The index of fibonacci number to be printed next */ 
        int current;  /* The value of the (i)th fibonacci number */
        int next;     /* The value of the (i+1)th fibonacci number */
        int twoaway;  /* The value of the (i+2)th fibonacci number */
    
        printf("How many Fibonacci numbers do you want to compute? ");
        scanf("%d", &n);
        if (n<=0)
           printf("The number should be positive.\n");
        else {
          printf("\n\n\tI \t Fibonacci(I) \n\t=====================\n");
          next = current = 1;
          for (i=1; i<=n; i++) {
     printf("\t%d \t   %d\n", i, current);
     twoaway = current+next;
     current = next;
     next    = twoaway;
          }
    }
    }
    
    void matrix()
    {
    int mat1[2][2],mat2[2][2],mat3[2][2],i,j,k;
    printf("************A program to find the multiplication of the given matrices***********\n\n"); 
    printf("************Enter the value for the 1st matrix************\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("\nEnter the value for %d row,%d column:",i+1,j+1); 
    scanf("%d",&mat1[i][j]); 
    } 
    } 
    printf("\n************Enter the value for the 2nd matrix************\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("\nEnter the value for %d row,%d column:",i+1,j+1); 
    scanf("%d",&mat2[i][j]); 
    } 
    } 
    printf("\n****************************The given matrices are*****************************\n\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("%d\t",mat1[i][j]); 
    } 
    printf("\n"); 
    } 
    printf("\n\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("%d\t",mat2[i][j]); 
    } 
    printf("\n"); 
    } 
    printf("\n****************************The multiplication of the matrices is*************\n\n"); 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    mat3[i][j]=0; 
    for(k=0;k<2;k++) 
    { 
    mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]); 
    } 
    } 
    } 
    for(i=0;i<2;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    printf("%d\t",mat3[i][j]); 
    } 
    printf("\n"); 
    } 
    
    }
    Last edited by lord_cedrich; 12-10-2006 at 12:55 AM.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Yeah, generally like that. Just make sure you've copied everything as needed and didn't leave anything out. I'm no expert as I'm learning myself, but there's often several ways to solve a particular problem. Does it compile and run successfully (excluding bugs, except crashers)?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Thank you very much i already solved my problem

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by lord_cedrich
    Thank you very much i already solved my problem
    more over think of changing your compile. That is one of ancient 16bit compiler. probably u might running it on 32bit machine. Try DEV-C++ or Borland C. I can also see some non standard library used in your code <conio.h>

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 05:03 PM
  2. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My first program source code
    By Musicdip in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2002, 11:55 PM