المصفوفات بلغة سي بلس بلس: مقدمة بسيطة


السلام عليكم ورحمة الله وبركاته اخوتي الكرام

اليكم في ادناه شرح برنامج بسيط بلغة

C++

لشرح بعض العمليات على المصفوفات

والبرنامج هو استجابة لطلب احد الاخوة المتابعين واتمنى ان يكون مفيداً للجميع

ملاحظة: البرنامج يعمل بشكل صحيح ومجرب وبعد الفيديو ستجدون الكود المصدري للبرنامج كاملاً

البرنامج:

Q: Write a program in c++ to define a 2D array of a size 5X5, then find the smallest sum of the rows and print it, and the biggest sum of columns and print it.

Solution:

#include<iostream>

#include<cmath>

using namespace std;

int main(){

int i,j,a[5][5], sum[5]={0,0,0,0,0}, sum1[5]={0,0,0,0,0}, temp=0;

cout<<“enter 5 X 5 array elements”<<endl;

for(i=0;i<5;i++)

for(j=0;j<5;j++)

cin>>a[i][j]; //input the array elements.

 

for(i=0;i<5;i++){

for(j=0;j<5;j++){

sum[i]=sum[i]+a[i][j]; }

cout<<sum[i]<<endl;    }

 

temp=sum[0];

for(i=0;i<5;i++){

if(sum[i]<temp)

temp=sum[i];}

cout<<“the least sum of rows is “<<temp<<endl;

 

for(i=0;i<5;i++){

for(j=0;j<5;j++){

sum1[i]=sum1[i]+a[j][i];}

cout<<“sum[“<<i<<“]=”<<sum1[i]<<endl; }

 

temp=sum1[0];

for(i=0;i<5;i++)

for(j=0;j<5;j++){

if(sum1[i]>temp)

temp=sum1[i];}

cout<<“the largest column sum is”<<temp<<endl;

 

    return 0;}

تحياتي للجميع ولا تنسوا المشاركة في النشر

18 comments

    •   اكتب برنامج C + لإنشاء مصفوفة متعددة تحتوي على صفين و 3 أعمدة بقيم عشوائية ، ثم احصل على الحد الأقصى والأدنى في هذا المصفوفة مع مواضعهم.

         كتابة برنامج C + لإنشاء مصفوفة متعددة تحتوي على صفين و 3 أعمدة بقيم عشوائية ، ثم الحصول على الحد الأقصى الأول والثاني.

        اكتب برنامج C + لإنشاء مصفوفة متعددة تحتوي على 4 صفوف و 4 أعمدة بقيم عشوائية ، ثم احصل على تجميع كل صف في عمود آخر وجمع كل عمود في صف آخر

      Like

  1. ليش كاتبه (cmath) بالبرنامج اذا ما تحتاجه
    البرنامج شغال ولكن عندي بعض الملاحظات
    1- انت تقدر تجمع الصفوف والاعمدة في حلقة وحدة يعني ماكو داعي حلقة ثانية
    2- ايجاد الاكبر والاصغر في حلفة وحدة لان مصفوفتين نفس الحجم

    Like

    • اهلاً وسهلاً اخي الكريم
      كل ما قلته صحيح والجواب ببساطة انه في البرمجة كل شيء ممكن وما نشرته فقط هو احد الطرق الكثيرة جداً للحل ولك ان تعتمد اي طريقة اخرى فهي مجرد تفضيلات تختلف من مبرمج لأخر 🙂

      Like

    • ممكن حل هذه الأسئلة

      اكتب برنامج لقراءة a[10]
      بحيث قيم عناصرها تكون عبارة عن اصفار ووحدات جد واطبع أطول عدد للواحدات المتتالية فيها؟

      اكتب برنامج لتكوين المتجه x[6]
      بحيث ام قيمة كل عنصر فيه هو حاصل مفكوك تسلسله(موقعه)؟

      Like

  2. Implementing Polynomials in C++

    • Write a program to represent a polynomail in an algebraic expression. As defined here, a polynomial consists of terms and each term consists of an integer coefficient and a nonnegative integer exponent. E.g.

    I. The Term
    • in the term 4X2, the coefficient is 4 and the exponent 2
    • in -6X8, the coefficient is -6 and the exponent 8

    II. The Polynomial

    Now create a c++ code to represent a polynomial. As defined here, a polynomial is a sequence of terms. E.g.

    1. 3X2 + 4X4 + X6
    2. 2 + 5X2 + 6X3 + 2X7
    3. 4X10

    The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10)

    III. The main method

    The main method will create display a menu that shows all polynomial operations and then read and process a series of operations until the user choose to exit the prohgram.

    The operations are:

    1. insert a new polynomial in its proper format, as shown here:

    3x^2 + 4x^4 + x^6

    2. return all the terms of a polynomial as a single line string, as shown here:

    3x^2 + 4x^4 + x^6

    3. compute and return the first derivative of all the polynomial
    4. compute and return the second derivative of all the polynomial

    Like

  3. ممكن حل سوال
    plane has 11 rows and each row has six seats for passengers as shown in the figure. Each seat is
    identified by the row number (1-11) and the seat character (A-F). When
    the program starts at first, all the seats are available. Then the program
    will read the already reserved seats from a file called reservations.txt
    (Attached) and mark those seats as reserved. The format of this file is as
    follow:
    (Row number) (Seat character)
    After that, the user will start entering seats numbers, and each time the
    system will check if seat number entered is free or not, if yes then the seat
    will be reserved and the program will display the message “Seat reserved
    successfully”. If seat number is already reserved, the system will display
    “Seat is not free, please enter another seat number”.
    The program will continue to work this way until all seats are reserved, or
    until user chooses to end the program. When the user choose to end the
    program, the program should write back all the reserved seats into the
    reservations.txt file in the same format mentioned above.
    Please note the following:
     Your program should apply the use of arrays.
     Your program should also apply the use of functions. There should be at least 2
    functions:
    o reserve: to reserve a seat using row number and seat character from the
    seats array.
    o printReserved: to print out all the reserved seats. You are free to print the

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.