Potrzebuje wytłumaczenia zadania

0

W tablicy A zapisz N liczb całkowitych wylosowanych z przedziału od 0..10000 .

Napisz zestaw funkcji realizujących następujące zadania:

Wyszukiwanie liczby pierwszej i wypisanie jej numeru w tablicy lub informacji o jej braku.
Zamiana położenia elementów tablicy A o wskazanych indeksach.
Sprawdzenie, która liczba w tablicy A jest liczbą palindromem i wypisywanie tej liczby wraz z jej indeksem.
Sumowanie cyfr elementów tablicy A i wpisywanie tych sum do innej tablicy B .
Wpisanie do tablicy C reprezentacji wylosowanych liczb w systemie o wskazanej podstawie p (gdzie 2<=p<=10).

0

Potraktuj to jako osobne zadania, każdym zajmij się po kolei.

Wyszukiwanie liczby pierwszej i wypisanie jej numeru w tablicy lub informacji o jej braku.

Czy masz z tym jakiś problem?

0

Potrzebuje zrobionego zadania a termin mam do 11 i czas mnie goni, znajomy z którym jestem w grupie zachorował a on był właśnie informatykiem w grupie i mam problem teraz.
Dodam, że operujemy code blocks na trzech bibliotekach Iostream, ctime, i cstdlib

0

Masz 50 minut na wykonanie tych zadań? Faktycznie ciężka sprawa.

0

Będąc całkowicie szczerym informatyka jest moim słabym atutem i jestem z niej co najmniej bierny

0
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <sstream>
#define n 5
using namespace std;

void dane(int T[])
{ srand(time(0));
  for (int i=0;i<n;i++) T[i]=rand()%10;
}

void druk(int T[])
{ for (int i=0;i<n;i++) { cout.width(6); cout << T[i];}
  cout <<endl;
}

bool czyP(int liczba)
{  if(liczba<2) return false;
    else{
        int i=2;
      while(i*i<=liczba)
      {  if(liczba%i==0)  return false;
          i++;
      }
    }
  return true;
}

void liczby_pierwsze(int T[])
{    for(int i=0;i<n;i++)
          if(czyP(T[i])) cout <<T[i] <<" numer=" << i<<endl;
}

void zamiana(int T[], int a, int b)
 {swap(T[a],T[b]);}

 int mini (int T[], int a, int b, int order)
 {
     int k=a; int m=T[a];
     bool relacja;

     for (int i=a; i<n;i++)
     {
           if (order==1) relacja=T[i]<=m; else relacja=T[i]>m;
           if (relacja) {k=1;m=T[i];}
     }
     return k;
 }



void sortuj_liczby(int T[])
{
    for (int i=0;i<n;i++) zamiana (T,i,mini(T,i,n,1));

}

bool testPal(int liczba)
{ int d=0;
  int x=liczba;
  while(x!=0)
  { x/=10;
    d++;
  }

int L[d];

int i=d-1;
  while(liczba!=0)
  {  L[i]=liczba%10;
     liczba/=10;
     i--;
  }
 for(int i=0;i<d;i++)
         if(L[i]!=L[d-i-1])  return false;
  return true;
}

jak na razie mam to ale nie jestem pewny czy jest to wykonane dobrze

0

Proszę o pomoc.

0

Jakbys sie troche postaral to pewnie znalazlyby sie osoby ktore by Ci pomogly...
Kod wrzuca sie pomiedzy specialny znacznik ```(trzy backticki)
A Tak nawet nikomu nie chce sie patrzec na kod bez wciec i wstawiony z d**y.

0

Tutaj masz rozwiazanie: https://wandbox.org/permlink/a2YiIWmcWbTpE4PO

// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <ctime>
#include <cstdlib>

#define N 10

/**
 * Check if given number is primal
 */
bool isPrimal(int n) 
{
    if (n < 2) {
        return false;
    }
    
    for (int i=2; (i*i)<=n; i++) { //because math lib is not availably in exercise
        if (n%i == 0) {
            return false;
        }
    }
        
    return true;
}

void findPrimals(int *numbers) 
{
    int found = 0;
    std::cout<<"Found primals: ";
    for (int i=0; i<N; i++) {
        if (isPrimal(numbers[i])) {
            std::cout<<i<<" ";
            
            found++;        
        }        
    }
    
    if (found <=0) {
        std::cout<<"not found";
    }
    
    std::cout<<std::endl;
}

void swapElements(int *numbers, int idx1, int idx2) 
{
    if (idx1 > N || idx2 > N) {
        return;
    }
    
    std::swap(numbers[idx1], numbers[idx2]);
}

void printArray(int *numbers)
{
    std::cout<<"Array: {";
    for (int i=0; i<N; i++) {
        std::cout<<" "<<numbers[i]<<" ";
    }
    std::cout<<"}"<<std::endl;
}


int log10(int v)
{
    return 
    (v >= 1000000000)   ? 9 : (v >= 100000000)  ? 8 : 
    (v >= 10000000)     ? 7 : (v >= 1000000)    ? 6 : 
    (v >= 100000)       ? 5 : (v >= 10000)      ? 4 :
    (v >= 1000)         ? 3 : (v >= 100)        ? 2 :
    (v >= 10)           ? 1 : 0; 
}

int pow(int x, int n) {
    if (n<1) {
        return 1;
    }
    
    int result = x;
    for (;n>1; n--) {
        result *= x;
    }
   
    return result;
}

bool isPalindrome(int number) 
{
    int digits = log10(number);
    int leftPos, rightPos;
    int leftVal, rightVal;
    
    for (rightPos = 1, leftPos=pow(10, digits); rightPos < leftPos; rightPos*=10, leftPos/=10) {
        
        leftVal = (number / leftPos) % 10;
        rightVal = (number / rightPos) % 10;
        
        if (rightVal != leftVal) {
            return false;
        }
    }
    
    return true;
}

void findPalindromes(int *numbers) 
{
    int found = 0;
    std::cout<<"Found palindromes: ";
    for (int i=0; i<N; i++) {
        if (isPalindrome(numbers[i])) {
            std::cout<<i<<" ";
            
            found++;        
        }        
    }
    
    if (found <=0) {
        std::cout<<"not found";
    }
    
    std::cout<<std::endl;
}

int sumDigitsInNumber(int number)
{
    int digits = log10(number);
    
    int sum = 0;
    for (int i=0; i<=digits; i++) {
        
        sum += (number/pow(10, i)) % 10;
    }
    
    return sum;
}

void sumDigitsOfNumbers(int *input, int *output)
{
    for (int i=0; i<N; i++) {
        output[i] = sumDigitsInNumber(input[i]);
    }
}

char *base10toBaseN(int number, int outputBase) 
{
    int divRes, iter = 0;
    char output[64];
    
    do {
        divRes = number / outputBase;
        output[iter] = (number - (divRes*outputBase)) + '0';
        
        number = divRes;        
        iter++;
    } while (divRes > 0); 
    
    
    char *finalOutput = new char(64);    
    
    for (int i=1; i<=iter; i++) {
        finalOutput[i-1] = output[iter-i];
    }   
    finalOutput[iter] = '\0';
    
    return finalOutput;
}

void convertArrayToBaseN(int *input, char **output, int base) 
{
    for (int i=0; i<N; i++) {
        output[i] = base10toBaseN(input[i], base);
    }
}

void printArrayOfChars(char **numbers)
{
    std::cout<<"Array: {";
    for (int i=0; i<N; i++) {
        std::cout<<" "<<numbers[i]<<" ";
    }
    std::cout<<"}"<<std::endl;
}

int main() 
{
    int arrayOfIntegers[] = {0, 1, 2, 13331, 4, 5, 66, 7, 1218, 121};
    int sumOfDigits[N] = {};
    char *convertedNumbers[N] = {};   
    
    
    findPrimals(arrayOfIntegers);    
    printArray(arrayOfIntegers);
    swapElements(arrayOfIntegers, 1, 2);
    printArray(arrayOfIntegers);
    findPalindromes(arrayOfIntegers);
    sumDigitsOfNumbers(arrayOfIntegers, sumOfDigits);
    printArray(sumOfDigits);
    convertArrayToBaseN(arrayOfIntegers, convertedNumbers, 2);
    printArrayOfChars(convertedNumbers);
    
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1