Hi thanks in advance for any help! I was wondering if there is a way to declare an array without knowing the size and letting the user input the amount of numbers they would like to enter, so letting the user input the size of the array? thanks.
#include <iostream>
usingnamespace std;
void bubbleSort(int, int);
bool binarySearch(int, int, int);
int size;
int naturals[];
int number;
int main()
{
cout << "Welcome to the Sort and Search program!" << endl;
cout << "Enter the number of naturals to be sorted: ";
cin >> size;
cout << "Enter the natural numbers:" << endl;
int count;
for (count = 0; count < size; count++)
{
cin >> naturals[number];
}
return 0;
}
Hi
i am not as good as others in c++ but i will try to build a dynamic array. I hope this will help you.
#include <stdio.h>
int main(int argc, char* argv[])
{
int row1,col1,col2,row2;
int i,j;
int **dump=NULL;
int ** dump2=NULL;
printf("\n Please enter the size of row col of the 1st matrix");
scanf("%d %d",&row1,&col1);
printf("\n Please enter the size of the row col of the second matrix");
scanf("%d %d",&row2,&col2);
printf(" %d %d ",row1,col1);
if (col1!=row2) {
printf("\nMatrix cannot be multiplied ");
getch();
exit(-1);
}
/* creating a dynamic array with the malloc function */
dump =(int **)malloc(sizeof(int)*row1);
for (i=0;i<row1;i++)
*(dump+i)=(int *)malloc(sizeof(int)*col1);
// for the second matrix
dump2 =(int **)malloc(sizeof(int)*row2);
for(i=0;i<row2;i++)
*(dump2+i)=(int *)malloc(sizeof(int)*col2);
// now taking the output from the user
printf("\n enter the values for the first matrix");
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
{
printf("\nEnter the valuesA[%d][%d] :",i,j);
scanf("%d",(*(dump+i)+j));
}
// taking the input of the second matrix
printf("\n enter the values for the second matrix");
for(i=0;i<row2;i++)
for(j=0;j<col2;j++)
{
printf("\nEnter the values B[%d][%d] :",i,j);
scanf("%d",(*(dump+i)+j));
}
This is not the full program but it is a very easy to understand example.