بسم الله الرحمن الرحيم
السلام عليكم ورحمة الله وبركاته
هذه بعض المسائل + الحلول حول Arrays و Structure و Pointers
Write a program to input 20 integer numbers into one dimensional array and then print on the screen the contents of the array and the smallest number inside it
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,myresult,mynumbers[20];
for ( x = 0; x < 20; x++ )
{
cout<< "Enter the number : " << x+1 << "\n";
cin>> mynumbers[x];
}
myresult = mynumbers[0];
for ( x = 1; x < 20; x++ )
{
if (mynumbers[x] < myresult)
myresult = mynumbers[x];
}
for ( x = 0; x < 20; x++ )
{
cout<< " Number : " << x+1 << " = " << mynumbers[x] << "\n";
}
cout<< " Smallest := " << myresult << "\n";
return 0;
}Write a C++ program to input 10 values into an array and then print on the screen the sum and the average of the values.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x;
float myresult,mynumbers[10];
for ( x = 0; x < 10; x++ )
{
cout<< "Enter the number : " << x+1 << "\n";
cin>> mynumbers[x];
myresult = myresult + mynumbers[x];
}
cout<< " Sum := " << myresult << "\n";
cout<< " Average := " << myresult/10 << "\n";
return 0;
}Write a program to enter 10 values into two dimensional array A and then compute the values of another two dimensional array B such that each element in B represents 5 times of the corresponding element in A and finally print out both the arrays A and B.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,y;
int myarrayA[5][2],myarrayB[5][2];
for ( y = 0; y < 5; y++ )
{
for ( x = 0; x < 2; x ++ )
{
cout<< "Enter the number : " << (y*2)+x+1 << "\n";
cin>> myarrayA[y][x];
myarrayB[y][x] = myarrayA[y][x] * 5;
}
}
for ( y = 0; y < 5; y++ )
{
for ( x = 0; x < 2; x ++ )
{
cout<< "Number : " << (y*2)+x+1 << "\n";
cout<< myarrayA[y][x] << "\n";
cout<< myarrayB[y][x] << "\n";
}
}
return 0;
}Write a program to input values of two dimensional arrays and then print on the screen the contents of the two arrays as well as their sum.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,y;
int myarrayA[5][2],myarrayB[5][2];
for ( y = 0; y < 5; y++ )
{
for ( x = 0; x < 2; x ++ )
{
cout<< "Array (A) : Row " << y+1 << " Col " << x+1 << "\n";
cin>> myarrayA[y][x];
cout<< "Array (B) : Row " << y+1 << " Col " << x+1 << "\n";
cin>> myarrayB[y][x];
}
}
for ( y = 0; y < 5; y++ )
{
for ( x = 0; x < 2; x ++ )
{
cout<< "Array (A) : Row " << y+1 << " Col " << x+1 << "\n";
cout<< myarrayA[y][x] << "\n";
cout<< "Array (B) : Row " << y+1 << " Col " << x+1 << "\n";
cout<< myarrayB[y][x] << "\n";
cout<< "Sum \n";
cout<< myarrayA[y][x] + myarrayB[y][x] << "\n";
}
}
return 0;
}Write a program to inputs the sales of a company in one month in an array and then find the average of the sales in the week and in the month as well as the sum of the sales and finally print out both the average and the sum.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,y;
float myarray[4][7],myresult[4][2];
float totalsum = 0;
for ( y = 0; y < 4; y++ )
{
for ( x = 0; x < 2; x ++ )
{
myresult[y][x] = 0;
}
}
for ( y = 0; y < 4; y++ )
{
for ( x = 0; x < 7; x ++ )
{
cout<< "Week " << y+1 << " Day " << x+1 << "\n";
cin>> myarray[y][x];
myresult[y][0] = myresult[y][0] + myarray[y][x];
}
myresult[y][1] = myresult[y][0]/7;
}
for ( y = 0; y < 4; y++ )
{
cout<< "Weel " << y+1 << "\n";
cout<< "Sum:" << myresult[y][0] << "Average:" << myresult[y][1] << "\n";
totalsum = totalsum + myresult[y][0];
}
cout<< " Sum in month : " << totalsum << "\n";
cout<< " Average in month : " << totalsum/28 << "\n";
return 0;
}Write a program to create a database for 100 students where each student is defined by stno, stname, staddress, and stage. Then input the data of 5 students and then print out these data.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
struct student
{
int stno;
char stname[50];
char staddress[50];
int stage;
} students[100];
int x;
for (x= 0; x<5; x++)
{
cout<< " Student : " << x+1 << "\n";
cout<< " Enter Student Number : ";
cin>> students[x].stno;
cout<< "\n Enter Student Name : ";
cin>> students[x].stname;
cout<< " \n Enter Student Address : ";
cin>> students[x].staddress;
cout<< "\n Enter Student stage : ";
cin>> students[x].stage;
}
for (x= 0; x<5; x++)
{
cout<< "Student : " << x+1 << "\n";
cout<< "Student Number " << students[x].stno << "\n";
cout<< "Student Name " << students[x].stname << "\n";
cout<< "Student Address " << students[x].staddress << "\n";
cout<< "Student stage " << students[x].stage << "\n";
}
return 0;
}Write a program to create a database of products where each product is defined by model, name, and price then enter the info of 3 products and then print out the database of the products.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
struct product
{
char name[50];
char model[50];
float price;
} products[100];
int x;
for (x= 0; x<3; x++)
{
cout<< " Product : " << x+1 << "\n";
cout<< "\n Enter Product Name : ";
cin>> products[x].name;
cout<< " \n Enter Product Model : ";
cin>> products[x].model;
cout<< "\n Enter Product Price : ";
cin>> products[x].price;
}
for (x= 0; x<3; x++)
{
cout<< "Product : " << x+1 << "\n";
cout<< "Product Name " << products[x].name << "\n";
cout<< "Product Model " << products[x].model << "\n";
cout<< "Product Price " << products[x].price << "\n";
}
return 0;
}Write a C++ program to input 10 integer values into an array using pointer and then print the content of the array on the screen
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,myarray[10];
int *mypointer;
mypointer = myarray;
for ( x=0; x<10; x++)
{
cout<< "Enter Number : \n";
cin>> mypointer[x];
}
for ( x=0; x<10; x++)
{
cout<< mypointer[x] << "\n";
}
return 0;
}حل اخر
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,myarray[10];
int *mypointer;
mypointer = &myarray[0];
for ( x=0; x<10; x++)
{
cout<< "Enter Number : \n";
cin>> mypointer[x];
}
for ( x=0; x<10; x++)
{
cout<< mypointer[x] << "\n";
}
return 0;
}حل اخر
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,myarray[10];
int *mypointer;
mypointer = myarray;
for ( x=0; x<10; x++)
{
cout<< "Enter Number : \n";
cin>> *(mypointer+x);
}
for ( x=0; x<10; x++)
{
cout<< *(mypointer+x) << "\n";
}
return 0;
}حل اخر
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,myarray[10];
int *mypointer;
mypointer = &myarray[0];
for ( x=0; x<10; x++)
{
cout<< "Enter Number : \n";
cin>> *(mypointer+x);
}
for ( x=0; x<10; x++)
{
cout<< *(mypointer+x) << "\n";
}
return 0;
}Using pointers write a C++ program to input 5 integer values into an array and find the smallest element and finally print out the array and the small number
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,myarray[5];
int *mypointer;
int myresult;
mypointer = myarray;
for ( x=0; x<5; x++)
{
cout<< "Enter Number " << x+1 << "\n";
cin>> mypointer[x];
}
myresult = mypointer[0];
for ( x=1; x<5; x++)
if (mypointer[x] < myresult)
myresult = mypointer[x];
for ( x=0; x<5; x++)
{
cout<< "\n" << mypointer[x] << "\n";
}
cout<< "\n Smallest = " << myresult << "\n";
return 0;
}Using pointers write a C++ program to input the data of student represented by stno, stname, staddress, stage and then print it onto the screen
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
struct student {
int stno;
char stname[50];
char staddress[50];
int stage;
} *mypointer;
student mystudent;
mypointer = &mystudent;
cout<< "Enter Student Number ";
cin>> mypointer->stno;
cout<< "\n Enter Student Name ";
cin>> mypointer->stname;
cout<< "\n Enter Student Address ";
cin>> mypointer->staddress;
cout<< "\n Enter Student Stage ";
cin>> mypointer->stage;
cout<< "\n Student Number : " << mypointer->stno;
cout<< "\n Student Nane : " << mypointer->stname;
cout<< "\n Student Address : " << mypointer->staddress;
cout<< "\n Student Stage : " << mypointer->stage << "\n";
return 0;
}By using pointers, write a C++ program to create a database of products as an array of structure (where each product is defined by model, name, and price) and then print the database on the screen.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
struct products {
char name[50];
char model[50];
float price;
} myproducts[3], *mypointer;
mypointer = myproducts;
int x;
for( x = 0; x<3; x++ )
{
cout<< "\n Enter Product Name ";
cin>> mypointer[x].name;
cout<< "\n Enter Product Model ";
cin>> mypointer[x].model;
cout<< "\n Enter Product Price ";
cin>> mypointer[x].price;
}
for( x = 0; x<3; x++ )
{
cout<< "\n Product Name : " << mypointer[x].name;
cout<< "\n Product Model : " << mypointer[x].model;
cout<< "\n Product Price : " << mypointer[x].price << "\n";
}
return 0;
}والله الموفق
مع تحياتى للجميع
م. محمود فايد