Thread: looking for answers before the final

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    17

    Unhappy looking for answers before the final

    hello every programmer here , I'm looking for answers for this questions so plz help me out

    1- write a program which read a string contains a number followed by an operation there anther number (e.g "25*3"), the program must preform the operation between the two numbers than print the result

    i know the reading and printing the array
    reading like that

    but we need 2 arrays one is char and the two is int
    lets say char A[6]
    and int B[6]



    Code:
    char A[6];
    for(i=0;(A[i]=getchar())!='\n';i++);
    A[i]='\0';

    Code:
    for(i=0;B[i]!='\0';i++)
    cout<<B[i];
    but the process still unknown


    i know that we can convert like that


    Code:
    for(i=0;A[i]!='\0';i++)
    n=n*10+A[i]-'0';

    but the operation ? and the result ?



    _____________________________________________

    2- write a c++ program to read A of 10 and B of 10 the program should compute the exert

    3 arrays of the same size
    A[10] , B[10],Z[10]

    Z for the numbers that found in A and B put it in Z



    also the processing

    Code:
    for(i=0;i<10;i++)
    if(A[i]==B[i])
    z[i];

    _____________________
    3- write a program to exchange the elements of second row with the elements with 3 row of array n*n

    Code:
    int n;int A[10][10],i,j,temp;
    
    cin>>n;
    for(i=0;i<n;i++)
    for(j=o;j<n;j++)
    cin>>A[i][j];
    
    for(i=0;i<n;i++)
    {
    temp=A[1][i];
    A[1][i]=A[n-1][i];
    A[n-1][i]=temp;
    }
    ________________________

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but we need 2 arrays
    Why arrays? Why not std::vector and std::string?

    Why all of the C code, you posted this in the C++ forum. Other than the minimal use of cout this is really more C than C++.

    By the way how are you insuring that you're not overflowing the bounds of your arrays? And you appear to have out of bounds access in several of your snippets.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    Quote Originally Posted by jimblumberg View Post
    Why arrays? Why not std::vector and std::string?

    Why all of the C code, you posted this in the C++ forum. Other than the minimal use of cout this is really more C than C++.

    By the way how are you insuring that you're not overflowing the bounds of your arrays? And you appear to have out of bounds access in several of your snippets.

    Jim
    cuz this what i study right now

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you're "looking for answers before the final" and you haven't studied either std::vector or std::string you should talk to your instructor and ask why these subjects haven't been covered.

    But as to your problems since you're trying to use raw arrays you need to manually insure that you never attempt to access these arrays out of bounds. No where in any of your code are you limiting the number of elements you try to retrieve into these arrays. This a very very bad habit that you must overcome. You should also avoid using the C stdio functions as much as possible, use C++ streams instead. The C++ streams are much safer to use than the C-stdio functions.

    Here is an example of possibly accessing your arrays out of bounds:

    Code:
       int n;
       int A[10][10], i, j, temp;
     
       cin >> n;   
       for(i = 0; i < n; i++)
          for(j = o; j < n; j++) // Note there is a typo on this line, do you see it?
             cin >> A[i][j];
    And note that code is basically C, with the exception of the cin, cout calls. Here is a more C++ way.
    Code:
       const int Array_size = 10;
       int A[Array_size][Array_size];
     
       int n;
       cin >> n;
       if( n <= Array_size) // Avoid accessing the array out of bounds.
       {
          for(int i = 0; i < n; i++)
             for(int j = 0; j < n; j++) 
                cin >> A[i][j];   
       }
    This is more C++ like because the variables are declared closer to the first use, not at one big glob at the beginning of the scope. Also note the use of a more sane indentation style and how the "program" is so much easier to read.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    Thanks Jim for your help and advice

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1- write a program which read a string contains a number followed by an operation there anther number (e.g "25*3"), the program must preform the operation between the two numbers than print the result

    i know the reading and printing the array
    reading like that

    but we need 2 arrays one is char and the two is int
    lets say char A[6]
    and int B[6]
    Even if you just wanted to use an array of integers to represent the operands in the problem, I don't see why you need so many. Two integers would be enough.

    If you were taught any way to convert text to integers, demonstrating that skill would be the first step to success. You need to parse, or convert, the first number, then extract the operator the user wants to use, followed by the second number. Then you need to use, say a long if/else tree to compare the operator to a reasonable set of options, and perform the calculation.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    17
    Quote Originally Posted by whiteflags View Post
    Even if you just wanted to use an array of integers to represent the operands in the problem, I don't see why you need so many. Two integers would be enough.

    If you were taught any way to convert text to integers, demonstrating that skill would be the first step to success. You need to parse, or convert, the first number, then extract the operator the user wants to use, followed by the second number. Then you need to use, say a long if/else tree to compare the operator to a reasonable set of options, and perform the calculation.

    this is how to convert the numbers

    Code:
    char s[10];
    int i,n
    
    for(i=0;(s[i]=getchar())!='\n';i++);
    s[i]='\0';
    
    for(i=0;s[i]!='\0';i++)
    n=n*10+s[i]-'0';
    than what?

    this is stop point of me ><"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-19-2013, 11:01 PM
  2. Please mark my answers
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 04-30-2012, 05:31 AM
  3. Please grade my answers
    By Richardcavell in forum C Programming
    Replies: 7
    Last Post: 04-28-2012, 11:15 PM
  4. Answers plz!!!
    By tipp2007 in forum C Programming
    Replies: 7
    Last Post: 12-14-2007, 02:03 AM
  5. DirectX dll Final Debug and Final Retail
    By hdragon in forum Tech Board
    Replies: 0
    Last Post: 11-15-2005, 09:46 PM

Tags for this Thread