Question

I need help fixing my code: In C++ *************** 1) I want to sum the digits...

I need help fixing my code: In C++ ***************

1) I want to sum the digits of an n*n matrix

2) find the average

I have completed the rest

****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it.

MY CODE: (I have indicated at which point I need help)

#include <iostream>

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

void bubbleSort(int arr[], int n)

{

int i;

int j;

for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

}

int main() {

int n;

cout<<"Please enter the n dimensions of your array: "<<endl;

cin>>n;

cout<<endl;

int arr[n][n];

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

arr[i][j] = rand() % 10;

}

}

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

cout << arr[i][j] << "";

}

cout << endl;

}

int largest = -100000;

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

if(*( *(arr + i) + j) > largest){

largest = *( *(arr + i) + j);

}

}

}

cout <<endl<< "The largest number is : " << largest << endl;

int smallest = 100000000;

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

if(*( *(arr + i) + j) < smallest){

smallest = *( *(arr + i) + j);

}

}

}

cout << "The smallest number is : " << smallest << endl;

int med[n*n];

int k = 0;

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

med[k++] = *( *(arr + i) + j);

}

}

cout<<endl<<"Listed digits from your array are : "<<endl;

for(int i=0; i<n*n; i++){

cout<<med[i];

}

cout << endl;

bubbleSort(med, n*n);

cout<<endl<<"The sorted digits from your array are : "<<endl;

for(int i=0; i<n*n; i++){

cout << med[i];

}

cout << endl;

cout <<endl << "The median digit is : "<< med[n*n/2]<< endl;

//*******I NEED HELP FROM THIS POINT*********

cout<< endl;

cout<<endl<<"The average value of the digits in your array is: "<<(med[n*n/2]+med[n*n/2 +1])/2<<endl;

cout<<endl;

cout<<endl<< "the sum of the digits in the arrray is: "<<endl;

for(int i=0;i<n*n;i++){

for(int j=0; j<n*n; j++){

cout<<i+j;

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

void bubbleSort(int arr[], int n)

{

int i;

int j;

for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

}

int main() {

int n;

cout<<"Please enter the n dimensions of your array: "<<endl;

cin>>n;

cout<<endl;

int arr[n][n];

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

arr[i][j] = rand() % 10;

}

}

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

cout << arr[i][j] << "";

}

cout << endl;

}

int largest = -100000;

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

if(*( *(arr + i) + j) > largest){

largest = *( *(arr + i) + j);

}

}

}

cout <<endl<< "The largest number is : " << largest << endl;

int smallest = 100000000;

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

if(*( *(arr + i) + j) < smallest){

smallest = *( *(arr + i) + j);

}

}

}

cout << "The smallest number is : " << smallest << endl;

int med[n*n];

int k = 0;

for(int i=0; i<n; i++){

for(int j=0; j<n; j++){

med[k++] = *( *(arr + i) + j);

}

}

cout<<endl<<"Listed digits from your array are : "<<endl;

for(int i=0; i<n*n; i++){

cout<<med[i];

}

cout << endl;

bubbleSort(med, n*n);

cout<<endl<<"The sorted digits from your array are : "<<endl;

for(int i=0; i<n*n; i++){

cout << med[i];

}

cout << endl;

cout <<endl << "The median digit is : "<< med[n*n/2]<< endl;

//*******I NEED HELP FROM THIS POINT*********

cout<< endl;

double sum=0;
int count=0;

//finds the sum of elements in the array
for(int i=0; i<n; i++)

for(int j=0; j<n; j++){
sum =sum+ *( *(arr + i) + j);
count++;
}

cout<<endl<<"The average value of the digits in your array is: "<<(sum)/count<<endl;

cout<<endl;

cout<<endl<< "the sum of the digits in the arrray is: "<<sum<<endl;

for(int i=0;i<n*n;i++){

for(int j=0; j<n*n; j++){

cout<<i+j;

}

}

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
I need help fixing my code: In C++ *************** 1) I want to sum the digits...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...

    Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() {     int SIZE = 10;     int target;     int begining=0;     int ending=SIZE-1;     int *array1= new int[SIZE];     cout << "Enter 10 integers: " << endl;     for (int i=0; i<SIZE; i++)     {        cin>>array1[i];     }     cout << " What is the Kth smallest number...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT