Int num2 l sarray i &0xf0 4 là gì năm 2024

import java.util.Scanner; public class Program { public static void main(String[] args) { int array[][] = {10, 5, 7, 2},{20,4,17,12}; System.out.println("Choose a value between 0-4"); Scanner scan = new Scanner(System.in); int num = scan.nextInt(); int num2 = scan.nextInt(); int value = array[num][num2]; System.out.println(value + " < Your selected Value"); } }

22nd Jul 2017, 7:16 AM

D_Stark

Int num2 l sarray i &0xf0 4 là gì năm 2024

The problem isn't with accessing the array it's with initializing the array. You use: int array[][] = {10, 5, 7, 2},{20,4,17,12}; What should be: int array[][] = {{10, 5, 7, 2},{20,4,17,12}}; Then it should work as expected.

include <iostream>

include <algorithm>

include <string>

using namespace std;

int min_sum(int *a, int len) { int size= len-1, flag=0; int unit1=0,unit2=0,tens1=0,tens2=0,hundreds1=0,hundreds2=0; int thousands1=0,thousands2=0,tenthousands1=0,tenthousands2=0; int num1=0,num2=0;

cout << "Total number of elements in array =" << len << "\n";

unit1=a[size]; size--; //cout << size; unit2=a[size]; size--; //cout << size;

if (size>=0) { tens1=a[size]; size--; }

if (size>=0) { tens2=a[size]; size--; }

if (size>=0) hundreds1=a[size]; size--;

if (size>=0) hundreds2=a[size--];

if (size>=0) thousands1=a[size--];

if (size>=0) thousands2=a[size--];

if (size>=0) tenthousands1=a[size--];

if (size>=0) tenthousands2=a[size--];

num1 = unit1 + tens1 * 10 + hundreds1 * 100 + thousands1 * 1000 + tenthousands1 * 10000; num2 = unit2 + tens2 * 10 + hundreds2 * 100 + thousands2 * 1000 + tenthousands2 * 10000;

for (int i=0;i<len;i++) { if (a[i]==0) flag++; }

int t1=num1,t2=num2;

while(t1) { t1=t1/10; cout << "\n t1=" << t1; }

if(flag--) num2=num2 * 10; if(flag--) num1=num1 * 10;

cout << "The two numbers which form min sum are:\n" << num1 << "\t" << num2 << "\n"; cout << "Minimum sum required = " << num1+num2 << "\n";

return 1;

}

int sort_it (int *a, int len) {

int i,j,temp; for (i=0;i<len;i++) { for (j=0;j<len-1-i;j++) { if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } }

cout<< "\n{\t";

for (int i=0; i<len; i++) { cout << a[i]<< "\t"; }

cout<< "\t}\n"; }

int main() { int array[]= {1,9,0,2}; //{9,2,7,5,7,2,1,5,4,6}; //{1,3,5,3}; //{1,2,7,8,9}; //{ 2,7,6,5,3,4,8,9,1};

int len= sizeof(array)/sizeof(array[0]);

cout<< "{\t";

for (int i=0; i<len; i++) { cout << array[i]<< "\t"; }

cout<< "\t}";

sort_it(array,len); min_sum(array,len);

return 0; }

Pass the array through to the function in the arguments list, you will also need to pass the size of the array through as a seperate item in the arguments list.

1
2
3
4
5
6
7
8
9
10
11
12
13
void foo(int array[], int size)
{
   for (int n = 0; n < size; ++n)
      array[n] = n*2;
}
int main() 
{
   int array[10];
   foo(array, sizeof(array)/sizeof(int));
   return 0;
}

Last edited on

A C-style array cannot be returned from a function. it can only be modified by reference or, as in ajh32's answer, through pointer to element.

1. I expect you to be able to manipulate the value of a variable of a simple type using a pointer. This is not particularly useful, it just demonstrates that you know how pointers work. For example, given integer variable num, write code to declare a pointer variable called numPtr and store the address of num in numPtr. Then write code to change the value of num to 5 and print it's value without using the variable name (use the pointer).

int num = 10; // given int *numPtr; numPtr = # *numPtr = 5; cout << *numPtr << endl; // prints 5 (the value of num) cout << num << endl; // prints 5 (the value of num)

2. The following function (shown with sample call) uses reference parameters to exchange the values of the arguments in the call. Re-write the function to use pointers instead of reference parameters (re-write the sample call also). Note: when you want to change the values of the arguments in a function call, you really should use reference parameters, not pointers. This is just an exercise to show how pointers work.

Call:

int x = 10, y = 20; swap( x, y );

function definition:

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

The revised code using pointers is:

Call:

int x = 10, y = 20; swap( &x, &y );

function definition:

void swap( int *num1, int *num2 ) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; };

3. I expect you to be able to dynamically allocate memory for a value of a simple data type. For example, write code to dynamically allocate memory for a float value, store 6.5 in it, and then print it's value.

float *fPtr; fPtr = new float; *fPtr = 6.5; cout << *fPtr; delete fPtr;

4. I expect you to be able to dynamically allocate an array of a simple data type. For example, write code to dynamically allocate an array of 5 integers, initialize the array to { 10, 20, 30, 40, 50 }, and then print the values in the array.

Note that you can use either array or pointer notation to access the dynamically allocated array (I use array notation).

int intArray; intArray = new int[5]; int index; for ( index = 0; index < 5; index++ ) { intArray[index] = ( index + 1 ) 10; cout << intArray[index]; } delete [] intArray;

5. I expect you to know the relationship between pointers and arrays in C++.(see section 10.3). Here are some notes that I wrote on the topic.

This is not a question - just some notes.

Questions 6 - 10 refer to this struct declaration:

struct Item { int itemNo; char desc[41]; float price; };

6. Write the declaration for a variable called itemPtr that can hold the address of an Item struct. Then dynamically allocate an Item struct and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

Item *itemPtr; itemPtr = new Item; itemPtr->itemNo = 10; strcpy( itemPtr->desc, "Granny Smith Apples" ); itemPtr->price = 1.89;

7. Write a function that takes as parameters a pointer to an Item struct, an item number, a description, and a price. Your function should assign the item number, description, and price to the struct data members.

void store( Item *itemPtr, int anItem, char aDesc[], float aPrice ) { itemPtr->itemNo = anItem; strcpy( itemPtr->desc, aDesc ); itemPtr->price = aPrice; }

8. Write a declaration for an array of 150 pointers to structs of type Item called itemPtrs. Then write code to dynamically allocate an Item struct, store it's address in the first element of itemPtrs, then set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

int x = 10, y = 20; swap( x, y );

0

9. Write a function that prints the descriptions of all the elements in array itemPtrs where the price is 2.0 or over. You can assume that the array is full (each element of the array points to an Item object).

int x = 10, y = 20; swap( x, y );

1

10. Write a function that returns a count of the number of Items in array itemPtr whose price is 2.0 or greater. You can assume that each element of the array points to an Item object.

int x = 10, y = 20; swap( x, y );

2

11. Write a declaration for a structure to hold a date as 3 integer values: the month, day, and year. Then write code to dynamically allocate a date, store some information in it, and print it out.

int x = 10, y = 20; swap( x, y );

3

A2. Reviw the following Example C++ Program that dynamically allocates structs and stores their addresses in an array of pointers:

itemptr.cpp

The following questions (12 - 17) refer to this class declaration:

int x = 10, y = 20; swap( x, y );

4

12. Class objects can be dynamically allocated similar to the way structs are dynamically allocated. Given the Item class declared above, write client code to create a variable to hold the address of an Item object. Then dynamically allocate an Item object and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

int x = 10, y = 20; swap( x, y );

5

13. Write client code to declare an array called itemPtrs of 150 pointers to Item objects.

int x = 10, y = 20; swap( x, y );

6

14. Write client code to dynamically allocate an Item object, store it's address in the first position of array itemPtrs, and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

int x = 10, y = 20; swap( x, y );

7

15. Write a function that prints the descriptions of all the elements in array itemPtrs where the price is 2.0 or over. You can assume that the array is full (each element of the array points to an Item object).

int x = 10, y = 20; swap( x, y );

8

16. Write a function that returns a count of the number of Items in array itemPtr whose price is 2.0 or greater. You can assume that each element of the array points to an Item object.

int x = 10, y = 20; swap( x, y );

9

17. Given that the Item class contains a second constructor that allows the client programmer to declare an Item object and specify it's initial values in a single statement, write a statement that dynamically allocates an Item object and initializes it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

0

A1. Review the following Example C++ Program that dynamically allocates class objects and stores their addresses in an array of pointers:

itemptr2.cpp

Chapter 11 More About Classes

1. Given the partial class definition:

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

1

You want to add a constructor function that sets each of the data members to zero. What is wrong with this constructor implementation?

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

2

The object that is being initialized by the constructor is passed implicitly to the constructor function. (Actually, a pointer to the object called this is passed.) The parameters to the function are just 2 integers, NOT the member variables of the object. This function just initializes its parameters to zero, and makes no changes to the object at all. In fact, since the parameters have the same names as the object member variables, you cannot access the member variables of the object without using the this pointer. The correct version of this function is:

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

3

2. Given the partial Point class from the previous problem, you want to add a set function that changes the data members to values specified by the client programmer. What is wrong with this implementation?

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

4

The const keyword makes this a constant member function. That means that the compiler will not allow the data members of the object that invokes this function to be changed. We need to remove the const keyword to allow new values to be stored in the data members:

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

5

Chapter 12 More about Characters, Strings, and the string Class

1. Write the declaration for a C-string (character array) called paperColor (it should be able to hold a 40 character color name plus the null terminator character). Then write code to input a value for paperColor from the keyboard. (You can assume that the input will not contain any blanks.)

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

6

2. Using your variable from question 3, write code to set variable paperPrice to 3.95 if paperColor is "blue".

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

7

3. Using your variable from question 3, write code to store "beige" in paperColor. Then write code to print the value of the variable (include a descriptive label).

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

8

4. Write the declaration for a C-string (character array) called sentence (it should be able to hold 80 characters plus the null terminator character). Then write code to input a value for sentence from the keyboard. (Assume that the input is on a line by itself and that it may contain blanks.)

void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; };

9

5. To copy one string into another, you should use the strcpy() function (the assignment operator will not work). To help you understand how C strings work, try to write your own version of the strcpy() function. Make it a void function. Your function prototype should look like this:

void strcpy( char toString[], char fromString[] );

int x = 10, y = 20; swap( &x, &y );

0

6. Given the code segment:

int x = 10, y = 20; swap( &x, &y );

1

What is printed if the input is "It sure is hot today."?

When the extractor operator is used to input a string, it will do the following:

  1. Read and discard any leading whitespace characters (blanks, tabs, and newlines).
  2. Read characters and insert them into the string until a white space character is encountered (the whitespace character will not be input).
  3. Add the null terminator character into the string.

When the member function getline is used to input a string, it will:

  1. Read characters and insert them into the string until it has read one less than its second argument, or up to the first newline character, whichever comes first.
  2. Insert the null terminator character at the end of the string.
  3. If the next character in the input stream is a newline, getline will read and discard it.

The output is:

int x = 10, y = 20; swap( &x, &y );

2

7. Write the declaration for a string object called paperColor. Then write code to input a value for paperColor from the keyboard. (You can assume that the input will not contain any blanks.)

int x = 10, y = 20; swap( &x, &y );

3

8. Using your variable from question 7, write code to set variable paperPrice to 3.95 if paperColor is "blue".

int x = 10, y = 20; swap( &x, &y );

4

9. Using your variable from question 7, write code to store "beige" in paperColor. Then write code to print the value of the variable (include a descriptive label).

int x = 10, y = 20; swap( &x, &y );

5

10. Write the declaration for a string object called sentence. Then write code to input a value for sentence from the keyboard. (Assume that the input is on a line by itself and that it may contain blanks.)

int x = 10, y = 20; swap( &x, &y );

6

11. Given the code segment:

int x = 10, y = 20; swap( &x, &y );

7

What is printed if the input is "It sure is hot today."?

The output is:

int x = 10, y = 20; swap( &x, &y );

2

Chapter 13 File I/O

1. Write code to input 10 names (which may contain spaces) from the keyboard and output the names to a file. Include the declaration for your file object and code to open the file. The file should be named "friends.txt".

int x = 10, y = 20; swap( &x, &y );

9

2.A file contains one line of information about each home that was sold in Austin last year. You do not care about the information in the file, but you do need to know how many homes were sold in Austin last year. Write C++ code to do the job. Include the declaration for your file object and code to open the file. The file is named "HomeSales.txt", and each line in the file is 65 characters or less.

void swap( int *num1, int *num2 ) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; };

0

Chapter 17 Linked Lists

1. Under what circumstances might it be better to use a linked list instead of an array of structs or objects? Under what circumstances might it be better to use an array of structs or objects instead of a linked list? Issues: max size, shrink/grow, editing (insert/delete), direct access versus traversal.

Size of the problem (amount of information required)

If you have a pretty good idea of the amount of data a program needs to handle, an array might be the best choice. Just be sure to declare the array to hold the maximum amount of information that the program might need to process. For example, if most users of an inventory control program will have 100 to 150 inventory items, and no users will have more than 200 items, you might declare an array of 200 inventory items.

On the negative side, the size of an array must be a constant. So the size of an array is fixed at compile-time. When you store data in an array, you set an upper limit on the number of data items that can be processed. The only limit on the size of a linked list is the size of the computer's memory. In situations where the number of data items that will need to be processed is unknown, a linked list may be a better choice.

As another example, suppose that some users of an inventory control program will have up to 200 inventory items. But other users may only have 50 inventory items. Allocating an array of 200 inventory items when you only need to store 50 wastes memory. In situations where the amount of data that different users will need to process varies a lot, a linked list might be a good choice. (Note: it is usually not practical to create a custom version of a program for each user. For example, would a company want to sell one version of a word processor to users who only need to create small documents and a different version to users who need to create larger documents?)

Editing sorted lists of data

Say you are keeping a list of inventory items where each item is represented as a structure or object. The list of items may change - items may be added to the list or removed from the list. You might want to keep the list in some pre-determined order - maybe sorted on the inventory item number.

If you store the list in an array of structs or array of objects, inserting or deleting inventory items can involve a substantial amount of work. For example, if you need to insert a new inventory item at index 95 of the array, all items from index 95 through the end of the list need to be copied to the next higher position in the array to make room for the new item. If you need to delete the item at index 80 in the array, you need to fill in the hole left by the deleted item by copying the item from index 81 into the vacant position, and each remaining item in the list must be copied to the next lower index in the array.

If the list is stored in a linked list, a new inventory item can be added by allocating a new struct or object for the new item, then changing links (pointers) in the list to add the item. Deleting an item in the list merely involves changing the links to bypass the deleted item and then freeing the deleted struct or object. With linked lists, you never have to move the data from one place to another when inserting or deleting. So linked lists can be much more efficient in this situation.

How do you convert an int number to an array?

Here is a simple method in java that you can use it to convert int to array of int..

static int[] Int_to_array(int n).

int j = 0;.

int len = Integer.toString(n).length();.

int[] arr = new int[len];.

while(n!=0).

arr[len-j-1] = n%10;.

Can an int be an array?

int array[]: This declares array as a traditional array of integers. It means that array is a fixed-size collection of integers stored in consecutive memory locations.

How to declare int array in C?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.

What data type is an array?

The array data type is a compound data type represented by the number 8 in the database dictionary. Arrays store a list of elements of the same data type accessed by an index (element) number. The term array is synonymous with the terms list, vector, and sequence.